Home > Software engineering >  webelement selenium - how to found an element with xpath and link with onclick
webelement selenium - how to found an element with xpath and link with onclick

Time:10-31

I try to get element like this :

webDriver.findElement(By.xpath("//a[contains(@onclick="submitData('updateShortcodeAdmin','')")]"));

or like this :

webDriver.findElement(By.xpath("//a[contains(@onclick=\"submitData(\'updateShortcodeAdmin\',\'\');")]\"));

or like this :

webDriver.findElement(By.xpath("//a[contains(@onclick=\"submitData(\'updateShortcodeAdmin\',\'\')\")]"));

and got the same Error :

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

What am I writing wrong?

Thanks !

CodePudding user response:

Please use the below xpath :

//a[contains(@onclick,'submitData('updateShortcodeAdmin')')]

Explanation :

You are using = in contains method, it should have been ,

CodePudding user response:

Try this:

webDriver.findElement(By.xpath('//a[contains(@onclick,"submitData('updateShortcodeAdmin'")]'));
  • Related