Reputation: 3801
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()
Upvotes: 2
Views: 168
Reputation: 52665
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')
Upvotes: 1