Reputation: 1516
Lets say I go to - https://www.expedia.co.uk/ I go to Trains Tab and do a search for any date with 1 passenger selected.
You'll be on next page where to select trains from a list, now if I want to click on any ShowFares button its not being recognized uniquely by CSS= .btn-secondary.btn-action
(its returning more than one matching node. So couldn't use it.
while using xpath -
.//*[@id='ember968']/div/div[1]/div[2]/div[2]/div/button
I see its recording @id
with some emberxxx
which again is not unique as its getting changed for every other search list..
Similarly when I clicked on ShowFare then unable to pick a train or fare as same above problem occurring as CSS is returning several nodes and xpath has this emberxxx which is not unique.
Upvotes: 0
Views: 681
Reputation: 1073
As with the same attributes, we have more than one element we are not able to pick right one. I tried with jquery selector .btn-secondary.btn-action:eq(1)
and it is working. By using above selector you will pick first Show Fares button every time Let me know if you have any queries.
CSS Selector: .flex-1up.flex-listing.flex-theme-light li:nth-child(1) button
Upvotes: 1
Reputation: 31
I see there are lot other elements having same xpath. Here is my suggestion if you want to click on first element.
//button[@class='btn-secondary btn-action']/span
- push that to list, loop through list and use getText()
. If matches 'Show Fares', click on that.
List<WebElement> buttonList = driver.findElements(By.xpath("//button[@class='btn-secondary btn-action']/span"));
for(int i=0; i<=buttonList.size() ; i++){
WebElement ele = buttonList.get(i);
if(ele.getText().contains("Show Fares"){
ele.click();
}
}
Upvotes: 0
Reputation: 95
Use, for example, xpath-function starts-with
:
(//*[starts-with(@id, 'ember')])[2]
This function find part of name. And then you can use filtr by []
to find needed element by index.
Upvotes: 1