Python 关于selenium你不知道的小技巧

在使用selenium 的时候我们通常通过以下几种方式去定位元素位置

browser = webdriver.Chrome(executable_path ='E:/chromedriver_win32/chromedriver')
				
browser.maximize_window()
				
browser.get(url)
#按照id索引,返回一个元素,有时并不精准(当元素没有id时)。
browser.find_element_by_id()
#返回多个元素
browser.find_elements_by_id()
#按照xpath索引,返回单个和多个元素
browser.find_element_by_xpath()
browser.find_elements_by_xpath()

但是当以上几种都无法定位到我们想要的元素的时候呢?坐标很显然是好办法,话不多说直接看代码

from selenium.webdriver.common.action_chains import ActionChains
#按照坐标点击
ActionChains(browser).move_by_offset(x,y).click().perform()
#点击输入框并输入。
ActionChains(browser).move_by_offset(x,y).click().send_keys('你的内容').perform()

完美执行。

你可能感兴趣的:(python,selenium,chrome)