我正在尝试使用硒中的driver.find_element_by_xpath
单击此page
我有以下一段代码,用作仅单击一个人名字的示例
python_button=driver.find_element_by_xpath("""//*[@id="search_results_people_search_832248975"]/div[3]/div[1]/div[1]/div[2]/a/h3""")
python_button.click()
但是当我运行它时会显示
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <h3 data-bind="text: personDispNm">...</h3> is not clickable at point (387, 558). Other element would receive the click: <div>...</div>
如何解决这个问题
答案 0 :(得分:1)
我不用单击即可获得超引用列表,然后导航到每个页面:
links = [link.get_attribute('href') for link in driver.find_elements_by_xpath('//a[h3]')]
for link in links:
driver.get(link)
# Do something on the page
答案 1 :(得分:1)
要通过网站https://www.dechert.com/content/dechert/en/people.html#firstName=&lastInitial=&lastName=&office=Philadelphia&page=1&q=&school=Villanova+University
上的xpath单击每个名称,您需要等待名称可见,然后收集 href 属性进行遍历依次如下:
代码块:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
hrefs= []
base_url = "https://www.dechert.com/content/dechert/en/people.html#firstName=&lastInitial=&lastName=&office=Philadelphia&page=1&q=&school=Villanova+University"
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get(base_url)
persons = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='name']//a[contains(@href,'/people/')]")))
for person in persons:
hrefs.append(person.get_attribute("href"))
for href in hrefs:
driver.get(href)
print(driver.current_url)
driver.get(base_url)
控制台输出:
https://www.dechert.com/people/b/april-banko.html
https://www.dechert.com/people/c/nicholas-carroll.html
https://www.dechert.com/people/e/william-elder.html
https://www.dechert.com/people/g/joe-gribbin.html
https://www.dechert.com/people/t/joseph-tate.html
https://www.dechert.com/people/t/marissa-tribuiani.html
也许您还可以使用@Andersson的解决方案,该解决方案经过如下修改后几乎是完美的:
links = [link.get_attribute('href') for link in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='name']//a[contains(@href,'/people/')]")))]
for link in links:
driver.get(link)
print(driver.current_url)
driver.get(base_url)
答案 2 :(得分:0)
这对我有用,您能否按以下方式更新xpath:
python_button = driver.find_element_by_xpath(
"//*[@id='search_results_people_search_832248975']/div[3]/div[1]/div[1]/div[2]/a/h3")
python_button.click()