Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable in xpath in Python Selenium

I've been having trouble figuring out how to get a variable to work Selenium. This post seems to have helped (Variable not working inside parenthesis) but I still can't get it to work.

When I used the actual value it works. In this case AL-Alabama. I created a variable called state so that I can just call that in my function. I have 13 states to run through.

driver.find_element_by_xpath("//option[@value='AL-Alabama']").click()

This one uses the state variable and in looking at error message it shows the variable value as AL-Alabama. So it seems like it's referencing the correct value in the web page. Not sure what I'm missing or why it's not working. Any guidance would be appreciated.

driver.find_element_by_xpath('//option[@value=' + state + ']').click()
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//option[@value=AL-Alabama]"}
like image 867
shavar Avatar asked Sep 30 '15 19:09

shavar


People also ask

Can we use variable in XPath in Selenium?

XPath is a strong tool in itself, but you can even use variables in your XPath expressions. The way it works is by referring to a variable as: $variable. You use the variable element to assign a value to a variable. Variables are case sensitive, so e.g. $var is not the same as $Var.

Can we use variables in XPath?

The variable element introduces a variable to be used in XPath expression in the subtree of the parent to the variable element. The name of the variable. This is a xsd:QName. [XPath 1.0] expression which is the value assigned to the variable.

How do you declare a variable in Selenium Python?

To declare a variable we use “=” to assign the value to a variable. Example: We will declare the following variable and print it.


2 Answers

To click() on the element with respect to the variable value attribute of the <option> tag using Selenium and python you can use either of the following Locator Strategies:

  • Using variable in XPATH:

    state = 'AL-Alabama'
    driver.find_element_by_xpath("//option[@value='" +state+ "']").click()
    
  • Using %s in XPATH:

    state = 'AL-Alabama'
    driver.find_element_by_xpath("//option[@value='%s']"% str(state)).click()
    
  • Using format() in XPATH:

    state = 'AL-Alabama'
    driver.find_element_by_xpath("//option[@value='{}']".format(str(state))).click()
    

Best Practices

Ideally. to click() on the element with respect to the variable value attribute of the <option> tag using Selenium] and you need to to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using variable in XPATH:

    state = 'AL-Alabama'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='" +state+ "']"))).click()
    
  • Using %s in XPATH:

    state = 'AL-Alabama'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='%s']"% str(state)))).click()
    
  • Using format() in XPATH:

    state = 'AL-Alabama'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='{}']".format(str(state))))).click()
    
  • Note : You have to add the following imports :

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

Reference

You can find a couple of relevant discussions in:

  • How to find an element with respect to the user input using Selenium and Python?
like image 142
undetected Selenium Avatar answered Oct 20 '22 21:10

undetected Selenium


The single quotes around the value are not present with how you coded it. Try:

driver.find_element_by_xpath("//option[@value='" + state + "']").click()
like image 17
EGHM Avatar answered Oct 20 '22 21:10

EGHM