What I am trying to do is to get all <checkBox>es, which belong to a <div>, which <div>s classname contains the string “something” and it’s text contains the string of a GlobalVariable.
To achieve this I am trying something like this:
WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> wElements = []
wElements = driver.findElements(By.xpath('//div[contains(@class, "something" and contains(text(), '+GlobalVariable.somethingElse+'))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]'))
for(WebElement element in wElements) {
String elementText = element.getText()
println(elementText)
}
And I get this syntax error:
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //div[contains(@class, "something" and contains(text(), Something Else))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, "something" and contains(text(), Something Else))]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]' is not a valid XPath expression.
Now, if I remove the part and contains(text(), '+GlobalVariable.somethingElse+') the test case ends successfully. So the syntax error should be here.
I have read this document and this article, and I have try some things like to type = instead of , after text() or to replace text(), with ., or to make it like this:
'//div[contains(@class, "datagrid-row song")][contains(text(), '+GlobalVariable.somethingElse+')]/div[@class="datagrid-cell cell-checkbox"]/label[@class="checkbox"]'
…but, nothing!!! I can’t understand what am I doing wrong here. If someone can see something…
Thank you for your time!!!
─[ EDIT 1]─────
After I changed:
[contains(text(), '+GlobalVariable.somethingElse+')]
To:
[contains(text(), "'+GlobalVariable.somethingElse+'")]
…I am not getting any errors but neither a result!!! So, something should be wrong with the text attribute of this elements now. Because the text I type into [contains(text(), '+GlobalVariable.somethingElse+')] exists for sure…
─[ EDIT 2]─────
To check if the outer div prints it’s text I try this:
WebDriver driver = DriverFactory.getWebDriver()
List<WebElement> wElements = []
wElements = driver.findElements(By.xpath('//div[contains(@class, "datagrid-row song")]'))
for(WebElement element in wElements) {
print(element.text)
}
And I get the text in three different lines like this!!!:
2022-02-22 13:34:34.681 DEBUG testcase.*** - 1: println(text)
18
La processione degli equinoziKeplero's House
05:59
Could this be the reason why it can’t find the text I am asking?