find_element_by_link_text does not work where the tah contains a space ( Pyhton Unitest Webdriver )

464 views
Skip to first unread message

PmGs

unread,
Nov 25, 2015, 12:31:54 AM11/25/15
to Selenium Users
Debian Jessie / Python3.4

------------- code that does not work at line : driver.find_element_by_link_text("Sign In").click()

#!/usr/bin/python3
from selenium import webdriver

import unittest, time, re

class Untitled(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://support.mozilla.org/"
   
    def test_untitled(self):
        driver = self.driver
        driver.get(self.base_url + "/en-US/products/firefox")
        driver.find_element_by_link_text("Sign In").click()

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

---------- Result ------------------
Traceback (most recent call last):
  File "./mozilla2.py", line 15, in test_untitled
    driver.find_element_by_link_text("Sign In").click()
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 282, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"link text","selector":"Sign In"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmp2iob05jk/extensions/fxdr...@googlecode.com/components/driver-component.js:10667)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmp2iob05jk/extensions/fxdr...@googlecode.com/components/driver-component.js:623)

SantoshSarma

unread,
Nov 25, 2015, 1:13:16 AM11/25/15
to Selenium Users
Yes, It has some leading & trailing spaces in it. link_text method looks for an anchor tag with given text as it is exact match.


In this case you can use below locators

find_element_by_partial_link_text("Sign In")
Or
find_elements_by_css_selector("a[href*='auth']")


PmGs

unread,
Nov 25, 2015, 4:07:34 AM11/25/15
to Selenium Users
Thanks for your answer.

I agree with your solution but the bug still remains. ( the sale code works fine with Selenium IDE for instance )

PmGs

unread,
Nov 25, 2015, 4:10:02 AM11/25/15
to Selenium Users
correction : the same code worksfine with Seleniul IDE

Le mercredi 25 novembre 2015 10:07:34 UTC+1, PmGs a écrit :
Thanks for your answer.

I agree with your solution but the bug still remains. ( the same code works fine with Selenium IDE for instance )

PmGs

unread,
Nov 25, 2015, 4:11:26 AM11/25/15
to Selenium Users
correction : see below

Le mercredi 25 novembre 2015 10:10:02 UTC+1, PmGs a écrit :
correction : the same code worksfine with Selenium IDE

Le mercredi 25 novembre 2015 10:07:34 UTC+1, PmGs a écrit :
Thanks for your answer.

I agree with your solution but the bug still remains. ( the sale code works fine with Selenium IDE for instance )
Reply all
Reply to author
Forward
0 new messages