开发者社区> 问答> 正文

selenium webdriver 元素定位问题?报错

在用selenium webdriver,元素定位遇到一个问题。

我想定位网页左侧导航栏中的第二栏用户查询:

对应的html是这样的:

 

我用python写的元素定位:
 

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

找的是id为container下面的第二个h1标签的a文字链接,但是报错了,不知道是不是javascript的原因,还有一点就是这个网页有iframe的结构

请问怎么找这个元素呢?试过link不行

展开
收起
爱吃鱼的程序员 2020-06-08 16:45:19 778 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    等待网页加载完成后,要使用switch切换到指定iframe,driver.switch_to_frame("xxx")

    感谢你的回复,切换到leftFrame就可以找到了。
    2020-06-08 16:45:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载