NosuchElement异常,当hidden_​​wait不起作用时,使用find_element_by_link_text? [英] NoSuchElement Exception using find_element_by_link_text when implicitly_wait doesn't work?

查看:148
本文介绍了NosuchElement异常,当hidden_​​wait不起作用时,使用find_element_by_link_text?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python和Selenium的新手,并为此进行编程.

New to python and Selenium and programming for that matter.

我正在尝试使硒自动化以击中特定链接.在这种情况下,我希望它点击与链接文本"B"关联的链接:

I am trying to automate selenium to hit a specific link. In this case, I want it to hit the link associated with the link text "B":

<li><a href="javascript:__doPostBack(&#39;ctl00$IndexControl1&#39;,&#39;B&#39;)">B</a>

在此网站上:

http://www.lw.com

我正在使用以下代码:

def get_single_link_using_find_elements_by_link_name(url, link_name):
    driver = webdriver.Firefox()
    driver.get(url)
    driver.implicitly_wait(10)
    time.sleep(20)
    element = driver.find_element_by_link_text(link_name)
    element.click()

我添加了一些等待条件,因为我认为该问题可能是渲染问题,但并没有帮助.

I added some wait conditions, because I thought the problem might have been a rendering problem, but they didnt help.

我遇到以下错误:

Traceback (most recent call last):
  File "C:\Python27\programs\selenium commands.py", line 50, in <module>
    get_single_link_using_find_elements_by_link_name(url, link_name)
  File "C:\Python27\programs\selenium commands.py", line 47, in get_single_link_using_find_elements_by_link_name
    element = driver.find_element_by_link_text(link_name)
  File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 237, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 671, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 156, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"B"}'

奇怪的是,相同的代码在以下URL上起作用,该URL是同一站点的一部分: http://www.lw.com/people?searchIndex=A

Oddly enough, the same code WORKS on the following url, which is part of the same site: http://www.lw.com/people?searchIndex=A

有什么想法吗?

推荐答案

  1. 您的代码对我有用,当我通过它时,该页面具有按字母顺序列出的索引.这意味着您将错误的变量传递给该函数-所传递的页面没有简单明了的名为'B'的链接.

您可以通过调用driver.find_element_by_id("IndexControl1")来检查字母是否在页面上. IndexControl1是包含字母的id的名称.

You can check whether or not the alphabetic thing is on the page by calling driver.find_element_by_id("IndexControl1"). IndexControl1 is the name of the id in which the alphabetic thing is contained.

alphabet = driver.find_element_by_id("IndexControl1")
link_b = alphabet.find_element_by_link_text("B")

  • 顺便说一句,需要注意的另一件事是,如果您已经在页面上选择了"B",例如http://www.lw.com/people?searchIndex=B&esmode=1,字母B不会显示为作为链接,在这种情况下,您也将以NoSuchElementException结尾.

  • Incidentally, something else to watch out for is that if you're already on the page with "B" selected, e.g. http://www.lw.com/people?searchIndex=B&esmode=1, the letter B does not show up as a link and you will end up with a NoSuchElementException in this case, as well.

    我认为这几乎涵盖了所有可能弹出NoSuchElementException的情况.祝你好运.

    I think that covers pretty much every case where NoSuchElementException could pop up. Good luck.

    这篇关于NosuchElement异常,当hidden_​​wait不起作用时,使用find_element_by_link_text?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆