2

I am trying to familiarize myself with Selenium and am running into an issue finding an element, I am noticing this a lot actually.

I want to go to yahoo.com, click on Sports, and assert the page title is correct. The following throws an "Unable to locate element" error message. I have tried ID, xPath etc. I have also tried other page elements, Mail, News etc...all throw the same error. Am I missing something here?

import unittest
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# go to Yahoo home page, click on Sports and assert title is correct

class searchMaps(unittest.TestCase):
    # def setup as class method
    @classmethod
    def setUpClass(inst):
        inst.driver = webdriver.Chrome()
        inst.driver.maximize_window()
        inst.driver.get("http://www.yahoo.com")
        inst.driver.implicitly_wait(20)

    def test_click_sports_page(self):
        self.search_field = self.driver.find_element_by_id('yui_3_18_0_3_1527260025921_1028')
        self.search_field.click()
        actual_title = driver.getTitle()
        expected_title = 'Yahoo Sports | Sports News'
        assertEquals(actual_title, expected_title)


    @classmethod
    def tearDownClass(inst):
        inst.driver.quit()


if __name__ == '__main__':
    unittest.main()
1
  • isn't this id yui_3_18_0_3_1527260025921_1028 dynamic and is supposed to change on next run ? Commented May 27, 2018 at 12:11

1 Answer 1

1

The @id of target link is dynamic, so it will be different each time you open the page.

Try to use link text to locate element:

self.search_field = self.driver.find_element_by_link_text('Sports')
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help. That did get me past that error, but now I am seeing a NameError: name 'driver' is not defined in relation to the following: actual_title = driver.getTitle()
Try actual_title = self.driver.title
ah, thank you. I am seeing other errors now but you solved my initial problem. Ill work through the others. Thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.