Reputation: 41
I try to click in a button like imagen.
not working using class or xpath
this is the inspect from this button.
this is the code trying to click on the button:
driver.findElement(By.xpath("/html/body/div[10]/button")).click();
this is the xpath from the before:
driver.findElement(By.xpath("/html/body/div[10]"));
please could someone help me!!!
Upvotes: 0
Views: 446
Reputation: 359
Every simple change in the page will cause your code to stop functioning, try to always make use of class or id and navigate to it's siblings/parent nodes.
You can do that in 2 ways
1.By using CssSelector (Right click on the Element in DevTools -> Copy -> Copy Selector)
driver.FindElement(By.CssSelector("CopiedText")).Click()
2.By using XPath and accessing it through it's parent (Example for your case)
driver.FindElement(By.xpath("//div[@class='advertising-layer']/button")).Click()
Upvotes: 1
Reputation: 193338
As per the HTML you have shared to invoke click()
on the desired element you have to induce WebDriverWait and you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("/div[@class='advertising-mask']//button"))).click();
Upvotes: 0
Reputation: 92
I experienced the similar issue while accessing a button in a dialog box. I tried with XPath, id, it didn't work but it worked with CSS selector. By using CSS selector I made the selenium webdriver to click on a button, pick a value from the drop-down in the dialog box, type a value in the text box. I am not sure why exactly it worked with CSS selector and not with XPath. I'd be grateful if someone has a description for this.
If you want to get CSS selector of a particular element follow the below steps
Select the element in the page to view the particular code for the inspector by using this button
right click on the highlighted part of the code and hover on copy, you will find CSS selector
Let me know if this works out for you.
Upvotes: 0