16

I am using Selenium with Python to automaticlly extract some data from our power plants and right now I need to click on an element. The problem is that the element's xpaths and order change for each plant we are monitoring. The only static info is the value, just like in the 3rd line value="T_U0.

I tried many approaches and I couldn't find a solution. I can't use index or child because the order of the parameters is changing. I tried CSS selector with no success.

Here you get some of my tries...

driver.find_element_by_xpath("//input[@value='T_U0']").click()
driver.find_element_by_css_selector("input[@data-id-sys-abbreviation='388']").click()

I tried many other things but I was just desperately trying anything.

What I really need is a find_by_value, if there is a way of doing it please let me know, if there isn't please show me how I can do it.

I need to click in some options that change order accordingly to the plant

4
  • 1
    Please don't post pictures of your code. just copy paste it in and use the code format button. Commented May 2, 2018 at 13:54
  • Possible duplicate of Selenium - Python - drop-down menu option value Commented May 2, 2018 at 13:55
  • @Jacobr365 can I copy the HTML from Chrome? bc I tried and I could copy only one line at a time and it got all messed up Commented May 2, 2018 at 13:57
  • Yes you can. in the inspect box select elements tab, then click on a block and right click copy. Commented May 2, 2018 at 13:59

3 Answers 3

19

The problem is with the first xpath. You are trying to locate an input while you need to get option.

Try this:

driver.find_element_by_xpath("//option[@value='T_U0']").click() 
Sign up to request clarification or add additional context in comments.

1 Comment

It did work! Thank you Anand, it was so simple and I couldn't see it, thank you again!
2

You can try to click/select element via displayed text. Pseudo code:

driver.find_element_by_xpath("//option[text()="Some text"]").click()

Comments

0

this works now:

driver.find_element(By.XPATH,'//*[@value="T_U0"]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.