Reputation: 44395
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 from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (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 from juvenile (P21-30) C57BL/6J male mice hippocampus CA1 pyramidal cell (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?
Upvotes: 0
Views: 363
Reputation: 1181
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]
Upvotes: 0
Reputation: 193338
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
Upvotes: 2