0

In a webpage I have the following elements:

....
<select class="widget-listbox form-control" size="6" multiple="multiple">
    <option data-value="sIPSCs%20from%20juvenile%20(P21-30)%20C57BL%2F6J%20male%20mice%20hippocampus%20CA1%20pyramidal%20cell%20(A1)" value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)" class="">sIPSCs&nbsp;from&nbsp;juvenile&nbsp;(P21-30)&nbsp;C57BL/6J&nbsp;male&nbsp;mice&nbsp;hippocampus&nbsp;CA1&nbsp;pyramidal&nbsp;cell&nbsp;(A1)</option>
    <option data-value="sIPSCs%20from%20juvenile%20(P21-30)%20C57BL%2F6J%20male%20mice%20hippocampus%20CA1%20pyramidal%20cell%20(A2)" value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A2)" class="">sIPSCs&nbsp;from&nbsp;juvenile&nbsp;(P21-30)&nbsp;C57BL/6J&nbsp;male&nbsp;mice&nbsp;hippocampus&nbsp;CA1&nbsp;pyramidal&nbsp;cell&nbsp;(A2)</option>
</select>
....

and I am trying the following xpath expression to select the first options element (for a selenium test):

//option[contains(text(),"sIPSC")]
//option[text()="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//option[@value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//select/option[contains(text(),"sIPSC")]
//select/option[text()="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]
//select/option[@value="sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)"]

but none shows any results (in chrome, XPath Helper Version 2.02; chrome version 70.0.3538.110). The element I look for is not inside a frame. I also want to select the whole string "sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)" which might be a variable. Just any string. I do not know beforehand how this string looks like...

What am I doing wrong this time? Shouldn't any of the above expressions work?

1
  • At least first one should work. Did you switch to frame? Show your code Commented Dec 3, 2018 at 17:59

2 Answers 2

2

To select the first option as the desired element are React element you need to induce WebDriverWait for the element to be clickable and you can use either of the following (Pythonic) solutions:

  • XPath 1:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(@data-value,'(A1)')]"))).click()
    
  • XPath 2:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(@value,'(A1)')]"))).click()
    
  • XPath 3:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='widget-listbox form-control']/option[contains(.,'(A1)')]"))).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
    
Sign up to request clarification or add additional context in comments.

1 Comment

No sorry, I want to select the element with the following complete string: "sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)" not just parts of it. Maybe I do not know that string beforehand...
0

Looks like both options contain "sIPSC"

Try an xpath that identifies what is unique about the element, like this: //option[contains(text(),"sIPSC")][contains(text(),"A1")]

If you will always want the first option: //select/option[contains(text(),"sIPSC")][1]

1 Comment

No sorry, I want to select the element with the following complete string: "sIPSCs from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (A1)" not just parts of it. Maybe I do not know that string beforehand...

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.