Python 如何通过网站上的xpath单击每个名称?
我正在尝试使用Python 如何通过网站上的xpath单击每个名称?,python,selenium,selenium-webdriver,xpath,webdriver,Python,Selenium,Selenium Webdriver,Xpath,Webdriver,我正在尝试使用驱动程序。通过selenium中的\u xpath查找\u元素\u,然后单击本文中列出的每个名称 我有下面的一段代码,它被用作只点击一个人名字的示例 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() 但是,当我运行此命令时,
驱动程序。通过selenium中的\u xpath
查找\u元素\u,然后单击本文中列出的每个名称
我有下面的一段代码,它被用作只点击一个人名字的示例
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>
引发异常\u类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.WebDriverException:消息:未知错误:元素。。。在点(387558)处不可单击。其他元素将收到单击:。。。
如何绕过此问题我将获得超级参考列表,然后导航到每个页面,而不是单击:
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
我不会单击,而是获取超级参考列表,然后导航到每个页面:
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
通过网站上的xpath单击每个名称
https://www.dechert.com/content/dechert/en/people.html#firstName=&lastInitial=&lastName=&office=Philadelphia&page=1&q=&school=Villanova+University
您需要等待名称可见,然后收集href属性以依次遍历,如下所示:
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)
- 代码块:
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)
https://www.dechert.com/content/dechert/en/people.html#firstName=&lastInitial=&lastName=&office=Philadelphia&page=1&q=&school=Villanova+University
您需要等待名称可见,然后收集href属性以依次遍历,如下所示:
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)
- 代码块:
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)
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()
这对我来说很有效,您能否按如下方式更新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()
您需要先滚动到元素。您需要先滚动到元素。这仅适用于OP希望单击所有链接时的第一个链接!是的,@Andersson这只适用于第一个链接。但由于他甚至不能点击第一个链接,这将是有益的。对于选择所有链接,他可以找到元素并按照您正确的建议进行迭代。这只适用于OP希望单击所有链接时的第一个链接!是的,@Andersson这只适用于第一个链接。但由于他甚至不能点击第一个链接,这将是有益的。为了选择所有链接,他可以找到元素并按照你正确的建议进行迭代。为什么你认为添加ExplicitWait会使我已经工作的解决方案“完美”?为什么你认为添加ExplicitWait会使我已经工作的解决方案“完美”?