Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium webdriver xpath InvalidSelectorError

I am new to selenium and I really have hard times here with dynamic buttons. I am writing selenium webdriver js scripts

I searched on stackoverflow and found few pages with similar errors posted but none of them solved my problems

I want to use this xpath

/dom[@domain='localhost:3000']//div[#'search-results']//button[@innertext='Add Contact']

and here is my code

driver.wait(until.elementIsVisible(driver.findElement(By.xpath("/dom[@domain='localhost:3000']//div[#'search-results']//button[@innertext='Add Contact']"))))

and get this error

InvalidSelectorError: invalid selector: Unable to locate an element with the xpa
th expression /dom[@domain='localhost:3000']//div[#'search-results']//button[@in
nertext='Add Contact'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '/dom[@domai
n='localhost:3000']//div[#'search-results']//button[@innertext='Add Contact']' i
s not a valid XPath expression.

and here is the html

<button class="btn btn-success btn-sm pull-right" data-email-id="[email protected]" data-user-id="[email protected]" data-name="user3 test">Add Contact</button>

Can you help me identify my problem ?

like image 645
Rasim AVCI Avatar asked Feb 19 '26 04:02

Rasim AVCI


1 Answers

#search-results can be used in CSS selectors for id attribute with value 'search-results'. In XPath you should use @id='search-results'

As @JeffC pointed out [@innertext='Add Contact'] predicate should be replaced with [.='Add Contact']

like image 190
Andersson Avatar answered Feb 21 '26 18:02

Andersson