首页 文章

selenium webdriver定位元素

提问于
浏览
1

我尝试使用selenium webdriver来定位元素:i want click the "用户查询"

对应的html:the red frame

蟒蛇:

>>> driver.find_element_by_xpath("//div[@id='container']/h1[2]/a").click()
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    driver.find_element_by_xpath("//div[@id='container']/h1[2]/a").click()
  File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
    'value': value})['value']
  File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "D:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.7.236900,platform=Windows NT 6.1 SP1 x86)

如何找到这个元素?

2 回答

  • 0

    使用索引在xpath下面尝试这个 .

    这里 . [1]表示 a 标记的索引号 . 假设在您的html中,有一些标签可以使用不同的名称 . 用户查询 . 假设,这个文本 用户查询 索引号是你的html中的一个然后在xpath下使用 .

    //h1[1]/a
    

    如果你的 html 中的这个文本 用户查询 索引号是两个,那么在xpath下面使用 .

    //h1[2]/a
    
  • 0

    您可以尝试找到元素based on it's link text

    driver.find_element_by_link_text("用户查询").click();
    

相关问题