简体   繁体   中英

Sikuli Java find a select option that contains specific text

I am trying to click on a specific option from a select statement using Sikuli in Java.

Given this select statement:

<select name="mySelect">
  <option value="ab">AB 273xc Some Text</option>
  <option value="cd">CD i8df4 Some More Text</option>
  <option value="ef">EF q43th And Text Once More</option>
</select> 

The following works if I know the full exact text contained in the option:

driver.findElement(By.xpath("//select[@name='mySelect']/option[.='CD i8df4 Some More Text']")).click();

The problem is that there is always random text within the string (i8df4 in this case) which changes every time, so I will not know what this text will be.

So I want to click on the option that contains the text "Some More Text" at the end. I see there is a CONTAINS function but I cannot figure out how to use it in this case (if its even possible to use).

So need to do some kind of pattern match or regex in the option part like this:

option[.='do pattern match here looking for Some More Text']

To test the value of a part of text you can use contains() . Therefore you may try this:

"//select[@name='mySelect']/option[contains(.,'Some Text')]"

But in my view it would be better to use the value attribute:

"//select[@name='mySelect']/option[@value='ab']"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM