简体   繁体   English

NosuchElement异常,当hidden_​​wait不起作用时,使用find_element_by_link_text?

[英]NoSuchElement Exception using find_element_by_link_text when implicitly_wait doesn't work?

New to python and Selenium and programming for that matter. python和Selenium的新手,对此进行编程。

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": 在这种情况下,我希望它点击与链接文本“ B”关联的链接:

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

on this website: 在此网站上:

http://www.lw.com http://www.lw.com

I am using this code: 我正在使用此代码:

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. 我添加了一些等待条件,因为我认为该问题可能是渲染问题,但它们没有帮助。

I am getting the following error: 我收到以下错误:

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"}'

Oddly enough, the same code WORKS on the following url, which is part of the same site: http://www.lw.com/people?searchIndex=A 奇怪的是,相同的代码在以下URL上起作用,该URL是同一站点的一部分: http : //www.lw.com/people?searchIndex=A

Any ideas? 有任何想法吗?

  1. Your code works for me, when I pass it a page that has the alphabetic listing index. 当我传递给您的代码具有字母列表索引的页面时,您的代码对我有用。 This means that you're passing the wrong variables to the function-- the page that you're passing in, doesn't have a link named 'B' , plain and simple. 这意味着您正在将错误的变量传递给函数-所传递的页面没有简单明了的名为'B'的链接。

  2. You can check whether or not the alphabetic thing is on the page by calling driver.find_element_by_id("IndexControl1") . 您可以通过调用driver.find_element_by_id("IndexControl1")来检查字母是否在页面上。 IndexControl1 is the name of the id in which the alphabetic thing is contained. IndexControl1是其中包含字母的事物的id的名称。

     alphabet = driver.find_element_by_id("IndexControl1") link_b = alphabet.find_element_by_link_text("B") 
  3. Incidentally, something else to watch out for is that if you're already on the page with "B" selected, eg 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. 顺便提一句,需要注意的是,如果您已经在页面上选择了“ B”,例如http://www.lw.com/people?searchIndex=B&esmode=1 ,则字母B不会显示作为链接 ,在这种情况下,您也将以NoSuchElementException结尾。

I think that covers pretty much every case where NoSuchElementException could pop up. 我认为这几乎涵盖了NoSuchElementException可能弹出的所有情况。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM