如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。
你应该检查的xpath:
//*[contains(text(), 'Options')]
或
//span[contains(text(), 'Options')]
检查步骤:
Press F12 in Chrome -> 转到element 部分 -> 执行CTRL + F -> 然后粘贴xpath 看看,如果您想要的element 用@ 得到突出显示 987654330@匹配节点。
在 Selenium 中有 4 种点击方式。
代码试用 1:
time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'Options')]").click()
代码试用 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Options')]"))).click()
代码试用 3:
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
driver.execute_script("arguments[0].click();", button)
代码试用 4:
time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
ActionChains(driver).move_to_element(button).click().perform()
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
另外,find_elements 返回一个列表,将其更改为 find_element 你应该可以继续。
或者使用列表索引来指向网页元素。
elems = driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
elems[0].click()
不推荐这种方式。