Home > Software design >  Cannot click on a button using SELENIUM and JAVA - triggering windows popup to upload file
Cannot click on a button using SELENIUM and JAVA - triggering windows popup to upload file

Time:10-05

I cannot click on a specific button.

Its name is "Select file" and it should open a windows popup:

<a >Select file</a>

The XPATH is:

//*[@id="div-add-file"]/a

I tried something like this but it seems not to be clicking anything and that's strange:

driver.findElement(By.xpath("//*[@id=\"div-add-file\"]/a")).click();

I also tried something like this:

driver.findElement(By.linkText("Select file")).click();

What's going on here?

CodePudding user response:

First note is that if possible, always tag the element itself with the id so your selectors are trivial.

Also, CSS selectors are usually more readable for humans. Under the hood, it's all xpath in the end, so it's good to be familiar with it, but it's not optimal. It's not really possible to diagnose your issue with the information provided. One possibility is that the selector is wrong, another is that the element is not loaded yet and you need to include a wait. A third possibility is your driver, or the browser version. Selenium has many things that can potentially go wrong

CodePudding user response:

If the class value is unique, then you can use:

driver.findElement(By.xpath("//*[@class='btn btn-primary btn-sm btn-upload']")).click()

(or)

driver.findElement(By.cssSelector(".btn.btn-primary.btn-sm.btn-upload")).click();

otherwise:

driver.findElement(By.xpath(".//a[contains(text(),'Select file')]")).click();
  • Related