david
david

Reputation: 6785

Why am I unable to select/click a bare text link with Selenium?

I am using Selenium web driver and Selenium IDE plugin for Firefox.

All I'm trying to do is click "United States" on the language page.

http://www.nike.com/language_tunnel

I am trying this (straight from the IDE recorder):

driver.find_element_by_xpath("(//button[@type='button'])[2]").click()
driver.find_element_by_link_text("United States").click()

Note that the first step works, it clicks on "AMERICAS", but the last step to click on "United States" errors out when I run it in Python with:

Unable to locate element: {"method":"link text","selector":"United States"}

What am I doing wrong here? Is there another way to select this link since find by link text apparently won't work here?

Upvotes: 1

Views: 56

Answers (1)

alecxe
alecxe

Reputation: 473873

Here are the things I've done to make it work:

  • changed the locator to a li.US a CSS selector
  • added a wait to wait for the visibility of the "US" link

The Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.nike.com/language_tunnel")

wait = WebDriverWait(driver, 10)

driver.find_element_by_xpath("(//button[@type='button'])[2]").click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li.US a"))).click()

Upvotes: 1

Related Questions