我想通过selenium库的location函数得到相应元素在屏幕位置,在用pyautogui,操作鼠标点击某个元素。
如:我想操作鼠标点击百度一下。(不要讲selenium可以操作元素,这个我知道,我知道selenium该怎么用,这里只是综合运用一下)
代码如下:
import pyautogui as pg
from selenium import webdriver
browser=webdriver.Chrome()
url='https://www.baidu.com'
browser.get(url)
time.sleep(2)
browser.maximize_window()
s=pg.position()
搜索一下 = browser.find_element_by_id('su')
搜索一下位置=搜索一下.location
pg.click(搜索一下位置['x'],搜索一下位置['y'])
print(s,搜索一下位置)
结果如下:
C:\Users\Win10\AppData\Local\Programs\Python\Python39\python.exe C:/Users/Win10/PycharmProjects/untitled5/练习包/试错2.py
Point(x=872, y=375) {'x': 1179, 'y': 222}
Process finished with exit code 0
其中s的值是我把鼠标放到百度一下按钮上的时候,相对于电脑屏幕左上角的位置。
第二点:屏幕已经调成了100%。
程序目的:selenium库识别元素位置,在用pyautogui库中的moveTo()函数去操作元素。
程序问题:识别出来的元素位置不能使用到moveTo()函数中去?请问解决方案?