Dima Bokov
Dima Bokov

Reputation: 101

How to locate relevant webElement by java selenium

Please see below attached enter image description herethe relevant XML code and my java code to locate the element:

Java code:

    //Set date range
    WebElement parentStartdate = obj.findElement(By.xpath("//[@id=typein_[Parameters].[Parameter 3]]"));
    WebElement child=parentStartdate.findElement(By.xpath("//input[@value='1/1/2019']"));

I get the follow error: Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[@id=typein_[Parameters].[Parameter 3]] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id=typein_[Parameters].[Parameter 3]]' is not a valid XPath expression.

Any help appreciated!

enter image description here

The whole xml structure: enter image description here

Upvotes: 1

Views: 124

Answers (3)

Constantin Trepadus
Constantin Trepadus

Reputation: 503

Try this selector:

//div[contains(@class, 'TypeInDiv')]/span[@class="TypeInQuerySpan"]/input[@type='text'][@class='QueryBox'][@value='1/1/2019']

the 3 identifiers on input are not mandatory, you can choose 1 or 2 or leave all 3, you decide.

Upvotes: 1

Sameer Arora
Sameer Arora

Reputation: 4507

If you have only one QueryBox on the webpage then you can select the element by using the following xpath for your case:

WebElement element = driver.findElement(By.xpath("//input[@class='QueryBox']"));

Upvotes: 1

Mate Mrše
Mate Mrše

Reputation: 8394

As the error message says, that is not a valid Xpath expression. Select the first element by

WebElement parentStartdate = obj.findElement(By.xpath("//span[@class='TypeInQuerySpan']/input"));

Upvotes: 1

Related Questions