Hariharbalaji
Hariharbalaji

Reputation: 2578

Getting org.openqa.selenium.InvalidSelectorException only for Chrome

I am tring to find a button in a webpage using find elements, the page can contain one of the below button ID's.

driver.findElements(By.xpath("//*[contains(@id,'topBtn')]")) driver.findElements(By.xpath("//*[contains(@id,'WSMplasticTop')]")) driver.findElements(By.xpath("//*[contains(@id,'bottomApplyBtn')]"))

The above code is working as expected when i use the Firefox Driver, where as getting the below error when i run in Chrome Driver.

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[contains(@id='bottomApplyBtn')] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(@id='bottomApplyBtn')]' is not a valid XPath expression.

Just wanted to know whether i have done any mistake

Upvotes: 0

Views: 488

Answers (2)

Paras
Paras

Reputation: 3235

When you are using contains method in your XPath expression, you basically are using an inbuilt method to find the element.

This method takes 2 parameters.

  1. First the tag you in which you want to search.
  2. Second is the actual text value which you are looking for inside the above tag.

Basically you have to call the method with 2 parameters and that 2 parameters should be comma separated.

So //*[contains(@id='bottomApplyBtn')] is wrong you should instead remove this = sign.

//*[contains(@id, 'bottomApplyBtn')]
              |_______|____________________ Parameter 1
                      |__________________________________Parameter 2

Hope it helps!

Upvotes: 1

Andersson
Andersson

Reputation: 52665

Try to use

'//*[contains(@id,'bottomApplyBtn')]'

instead of

'//*[contains(@id='bottomApplyBtn')]'

Upvotes: 2

Related Questions