0

I want to click a button(to send a form)

<button class="form-button primary">Click here</button>

I find the element like this:

driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;

On chorme is working and sending the form but in IE11 is not(sending the form). To be clear, in IE is finding the element(or an element). But probably is not the correct element.

Addition info:

  • This is the only button with this text

  • I can probably find other ways to get this element , but if I rework this path I will need to change all the paths similar to this.

Selenium version:3.14 IE webdriver :3.14

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Can you please inform us, how did you confirm that code finding the correct element in IE browser? Did you debug the code? Try to check the console to see whether there is any error or warning message. It can help to give the hint for the issue. – Deepak-MSFT Jan 22 '20 at 07:35
  • Well, it was the single element in the page that could have been taken by that XPath, and the run took an element with the above code.(driver.findElement(By.xpath("//button[contains(text(),'Click here')]"))) – Ciprian Manta Jan 22 '20 at 21:13

2 Answers2

2

There are several different ways of clicking on something using selenium. I would try using either a javascript click or an action click.

Javascript click:

WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

Action click:

Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();

It's also possible that you are in the wrong frame while you are executing the click.

driver.switchTo.frame("Frame_ID");

You would be able to find the frame ID when you inspect the webpage.

dom-frost
  • 36
  • 3
1

If your usecase is to invoke click() you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath using className and text:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='form-button primary' and text()='Click here']"))).click();
    
  • xpath using text:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Click here']"))).click();
    
  • xpath using contains():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(., 'Click here')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I've tried it ,but it didn't change the fact that it doesn't send the form.Plus if this was the issues I was expecting to give me an error like :"the element is not ready to be clicked".But thanks for the help. – Ciprian Manta Jan 22 '20 at 13:01