Reputation: 143
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 ?
Upvotes: 4
Views: 310
Reputation: 193338
The xpath
you used needs some modifications. You may skip the top level tags like dom
and their attributes
like domain
and still construct an unique cssSelector
or xpath
. More over #
is used while constructing cssSelector
. Having said that, I would like to avoid including the text part Add Contact
in our xpath
as well:
/dom[@domain='localhost:3000']//div[#'search-results']//button[@innertext='Add Contact']
Looking at the HTML
you shared we can easily construct a unique cssSelector
or xpath
to identify the element as follows using only the class
attribute as follows:
cssSelector
:
driver.findElement(By.cssSelector("button.btn.btn-success.btn-sm.pull-right"));
xpath
:
driver.findElement(By.xpath("//button[@class='btn btn-success btn-sm pull-right']"));
Upvotes: 0
Reputation: 52685
#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']
Upvotes: 1