Python 为什么我不能点击 Selenium 中的元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16511059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Cant I Click an Element in Selenium?
提问by Neil Aggarwal
I am trying to click an element in Selenium.
我正在尝试单击 Selenium 中的一个元素。
The site is: url = "http://jenner.com/people"
该站点是: url = " http://jenner.com/people"
The xpath for the element is: url = //div[@class='filter offices']
元素的 xpath 是: url = //div[@class='filter office']
Here is my code:
这是我的代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_xpath("//div[@class='filter offices']")
element.click()
When I click the element, the drop down for offices should appear. Instead, when I click the element, nothing happens. What am I doing wrong?
当我单击该元素时,应出现办公室下拉菜单。相反,当我单击该元素时,什么也没有发生。我究竟做错了什么?
采纳答案by Walery Strauch
You are clicking on div that contains other div with event listener. You should click on div where listener ist registered. This xpath should work:
您正在单击包含具有事件侦听器的其他 div 的 div。您应该单击注册侦听器的 div。这个 xpath 应该可以工作:
//div[@class='filter offices']/div[@class='header']
回答by Piyush
Here, I give you working script which select location.
在这里,我为您提供选择位置的工作脚本。
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()

