2

New to python and Selenium and programming for that matter.

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>

on this website:

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

Any ideas?

kenorb
  • 155,785
  • 88
  • 678
  • 743
Neil Aggarwal
  • 511
  • 1
  • 10
  • 29

1 Answers1

1
  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.

  2. 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")
    
  3. 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.

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

kreativitea
  • 1,741
  • 12
  • 14
  • Thanks for you help. The problem I am facing is that I know that the "B" link is on http://www.lw.com because I can see it when I pull up the source code. Yet, when I run my code on http://www.lw.com, I get the error above. – Neil Aggarwal Oct 16 '12 at 01:43
  • It doesn't work on lw.com because `find_element_by_link_text` and `.click()` both specify visible objects. `find_element_by_link_text` will fail to return something if the text isn't rendered in the browser. Just because it's in the source doesn't make it visible in the browser. The "B" link you're referring to is part of the javascript enabled 'People Finder' button at the top of the screen. You need to use `driver.find_element_by_id("PeopleFinderLink")` and THEN `driver.find_element_by_link_text("B").click()` to click on the desired link. – kreativitea Oct 16 '12 at 02:36
  • 2
    @NeilAggarwal it's considered courteous to either upvote or 'accept' the answer when one is helpful. :) Welcome to stackoverflow. – kreativitea Oct 16 '12 at 04:22