我正在编写一个脚本,我需要自动查找元素的光标位置。该脚本将在不同的计算机上运行,因此我希望它自动完成。
假设我想点击 stackoverflow.com 头部的搜索框,使用 selenium
和 pyautogui
找到它的位置。
如何做到这一点,以便鼠标位于该位置并退出脚本?
编辑 1
假设我希望它点击这个元素:
input
whichname="q"
andclass="s-input s-input__search js-search-field "
编辑 2:
我当前的代码在Prophet
和未检测到的Selenium
的帮助下,但我得到的输出不正确:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
from time import sleep
from datetime import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
path = r'path'
user_data = r'user'
options = webdriver.ChromeOptions()
options.add_argument(f'--user-data-dir={user_data}')
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=path, options=options)
actions = ActionChains(driver)
xpath = '//*[@id="search"]/div/input'
url = 'https://stackoverflow.com/'
driver.get(url)
position = WebDriverWait(driver, 20).until(ec.visibility_of_element_located((By.XPATH, xpath))).location_once_scrolled_into_view
# both tried the above position var and the below
# position = WebDriverWait(driver, 20).until(ec.visibility_of_element_located((By.XPATH, xpath))).location
# element = driver.find_element(By.XPATH, xpath)
# actions.move_to_element(element).perform()
# position = pyautogui.position()
file = open('position.txt', 'a')
file.write(f'{position}, {datetime.now()}\n')
driver.close()
# to test the exact or relative position to compare with above code
# while True:
# sleep(1)
# print(pyautogui.position())
编辑3:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
from time import sleep
from datetime import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
path = r'D:\\Downloads\\chromedriver_win32\\chromedriver.exe'
user_data = r'C:\\Users\\Saeed\\AppData\\Local\\Google\\Chrome\\User Data\\Default'
options = webdriver.ChromeOptions()
options.add_argument(f'--user-data-dir={user_data}')
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=path, options=options)
actions = ActionChains(driver)
xpath = '//*[@id="search"]/div/input' # `search` box at header
url = 'https://stackoverflow.com//'
driver.get(url)
driver.maximize_window()
sleep(10)
position = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, xpath))).location
print(position)
file = open('position.txt', 'a')
file.write(f'{position}, {datetime.now()}\n')
# pyautogui.click(793, 9)
driver.close()
# while True:
# sleep(1)
# print(pyautogui.position())
视频我的代码现在如何工作:https://up.mazandhost.com/uploads/1028407950.bandicam-2022-03-27-21-33-27-729.mp4
我的案例的解决方案:
最佳答案
通过 Selenium
,您可以使用 action_chains
库执行鼠标操作。
一旦您找到带有 selenium 的元素,您就可以将鼠标移动到该元素,如下所示:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
element = driver.find_element(By.XPATH, 'the_xpath_locator')
actions.move_to_element(element).perform()
关于python - 如何使用 pyautogui 和 selenium 自动查找元素的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71637610/