Home > Back-end >  Xpath python contains value
Xpath python contains value

Time:10-12

this is the html:

<select name="name1" id="id1" class="class1" size="2" ondblclick="moveoption(this.id,'atmSelezionati',this.selectedIndex);" tabindex="40">
<option value="12345|0101">12345/0101</option>
<option value="64534|0102">64534/0102</option>

I'm trying to select the option elements by having only for example '0101' which is a part of the value tag.

This is what I'm trying but it says "unable to locate element":

driver.find_element_by_xpath("//*[contains(option/@value,'"  "|"   '0101'  "')]")

CodePudding user response:

This drop down is build using select and option tag, Please use Selenium Select class :

select  = Select(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "id1"))))
select.select_by_value('12345|0101')

Imports :

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

Update :

index_selection = 1
driver.execute_script(f"return document.getElementById('id1').selectedIndex = '{index_selection}';")

CodePudding user response:

Solution:

I found this workaround:

driver.find_element_by_xpath("//option[contains(text(),'"  "/"   'your text'  "')]")
  • Related