为什么我不能点击Selenium中的元素?

Nei*_*wal 7 python selenium xpath click

我想点击Selenium中的一个元素.

该网站是:url =" http://jenner.com/people "

元素的xpath是:url = // div [@ class ='filter offices']

这是我的代码:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_xpath("//div[@class='filter offices']")
element.click()
Run Code Online (Sandbox Code Playgroud)

单击该元素时,应显示办公室的下拉列表.相反,当我点击元素时,没有任何反应.我究竟做错了什么?

Wal*_*uch 7

您正在单击包含具有事件侦听器的其他 div 的 div。您应该单击注册侦听器的 div。这个 xpath 应该可以工作:

//div[@class='filter offices']/div[@class='header']
Run Code Online (Sandbox Code Playgroud)

  • 在开发者工具中打开网站。例如,在我的 Firefox 中,元素附近有一个气泡**事件**。如果您单击该气泡,您将获得更多详细信息。如果没有气泡,请在树的上方或下方搜索正确的元素,直到找到为止,唯一重要的是该元素位于鼠标光标下方。 (3认同)
  • 你怎么知道监听器事件是在哪里注册的? (2认同)

Piy*_*ush 5

在这里,我给你选择位置的工作脚本.

from selenium import webdriver
import time

driver = webdriver.Chrome('./chromedriver.exe')
url="https://jenner.com/people"
try:
    driver.get(url)
    element = driver.find_element_by_xpath("//div[@class='filter offices']")
    element.click()
    time.sleep(5)
    element = driver.find_element_by_xpath("//input[@id='search_offices_chicago']")
    element.click()
    time.sleep(5)
except Exception as e:
    print e
    driver.quit()
driver.quit()
Run Code Online (Sandbox Code Playgroud)