代码之家  ›  专栏  ›  技术社区  ›  Nathan123

如何通过xpath在网站上单击每个名称?

  •  0
  • Nathan123  · 技术社区  · 6 年前

    我想用 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>
    

    我该如何解决这个问题

    3 回复  |  直到 6 年前
        1
  •  1
  •   undetected Selenium    6 年前

    通过网站上的xpath单击每个名称 https://www.dechert.com/content/dechert/en/people.html#firstName=&lastInitial=&lastName=&office=Philadelphia&page=1&q=&school=Villanova+University 你需要等待 姓名 然后收集 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
  •  1
  •   Andersson    6 年前

    我不会单击,而是获取超级引用列表,然后导航到每个页面:

    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
    
        3
  •  0
  •   iamsankalp89    6 年前

    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()