Using a variable in xpath in Python Selenium
Asked Answered
K

4

13

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]"}
Kisumu answered 30/9, 2015 at 19:56 Comment(3)
I marked the post by @EGHM as the answer. But for those who may be able to use this information I did find that Select also does indeed work. I used Select(driver.find_element_by_class_name("c3")).select_by_visible_text(state). Not sure if I was supposed to put code in a comment.Kisumu
@Kisumu going with a Select abstraction is indeed a much cleaner way to approach the problem.Ordonez
@ alecxe I appreciate your input. I was curious if Select was the preferred method. Going forward where possible I'll use Select abstraction. I'll have to do a bit more reading on its use of course.Kisumu
E
18

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

driver.find_element_by_xpath("//option[@value='" + state + "']").click()
Encrata answered 30/9, 2015 at 20:20 Comment(3)
Yep that did it. So I guess it wasn't reading it as a string? This will help me with my other projects. Thank you.Kisumu
I think python was reading it as a string. I know from lots of experience those quotes around values after the equals sign are required by Selenium.Encrata
Super-helpful. Thanks.Astigmia
Q
5

To click() on the element with respect to the variable value attribute of the <option> tag using Selenium and 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:

Quinque answered 4/6, 2020 at 21:23 Comment(0)
D
0

If you still want to use single quotes then you can also try this answer. Check if it helps:

driver.find_element_by_xpath('//option[@value=\'{State_Name}\']'.format(State_Name=State)).click()

or simply

driver.find_element_by_xpath('//option[@value=\'{}\']'.format(State)).click()

You can pass "State" as parameter in your function.

Diseuse answered 20/1, 2020 at 11:28 Comment(0)
R
0

A simple way could be to use the format string "f" and add the variable_name in {} driver.find_element_by_xpath(f"//List[@AutomationId=\"TileGridView\"]//ListItem[@ClassName=\"GridViewItem\"][@Name=\"{variable_name}\"]....")

Roentgenotherapy answered 10/3, 2022 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.