python - 如何使用 pyautogui 和 selenium 自动查找元素的位置

标签 python selenium pyautogui

我正在编写一个脚本,我需要自动查找元素的光标位置。该脚本将在不同的计算机上运行,​​因此我希望它自动完成。

假设我想点击 stackoverflow.com 头部的搜索框,使用 seleniumpyautogui 找到它的位置。

如何做到这一点,以便鼠标位于该位置并退出脚本?

编辑 1

假设我希望它点击这个元素:

input which name="q" and class="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

我的案例的解决方案:

回答https://stackoverflow.com/a/56693949/5790653帮助我。

最佳答案

通过 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/

相关文章:

python - QTimeEdit 当分钟减少时如何减少到前一小时?

python - 使用 Binned X 值制作条形图 Python

Python - 人类喜欢鼠标的行为

javascript - 复选框值的嵌套循环,与输入表单和以 json 格式保存的表单值进行比较。以及如何避免复选框组中的重复项

python - 打开和读取文件

azure - Azure DevOps 中的 .Net Core Seleniun 测试 - 没有文件与 .Net 还原任务的搜索模式匹配错误

java - 何时在 Selenium Webdriver 中使用显式等待与隐式等待?

java - 从 Selenium 中的内部标记中提取文本

python - 是否可以在 headless (headless)模式下运行 PyAutoGUI?

python - 单击程序时遇到问题 - pyautogui