1

I'm trying to click on an element but I always get the error "cannot locate an element using..". I've tried it with find by class, by csselector, and by XPath. I also tried the a class first and then the span class element but it's still not working. It's definitely the correct frame, too. It's really weird because it worked two weeks ago, I didn't change anything in the code and now it's not working. Is it possible that the element is constantly changing? If so, how can I make sure that it still finds the element without adjusting my code every time?

<a class="ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all" href="#" aria-label="Close" role="button">  
 <span class="ui-icon ui-icon closethick">
  ::after
 </span>
</a>

This is my current code now which still isn't working:

driver.switchTo().defaultContent();
    driver.switchTo().frame("frame_vehicleFileTab");
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    WebElement closePrint = (new WebDriverWait(driver, 10)).until(
            ExpectedConditions.elementToBeClickable(By.xpath("//*[@aria-label='Close']")));
    closePrint.click();

After trying DebanjanB's suggestion by searching for the element:

driver.findElement(By.xpath("//a[@class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all' and @aria-label='Close']")).click();

I get this error: org.openqa.selenium.ElementNotInteractableException: Element could not be scrolled into view

Update: I fixed it by getting the Selenium IDE extension for Firefox and then choosing the xpath that was generated by the extension together with the javascript executor:

WebElement closePrint = (new WebDriverWait(driver, 10)).until(
            ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='FileTab:Form:j_id674351400_da78919']/div/a/span")));
    JavascriptExecutor js1 = (JavascriptExecutor)driver;
    js1.executeScript("arguments[0].click();", closePrint);

I don't know why that xpath works now but I'm glad it does. Thanks everyone for your suggestions!

yoopaa
  • 29
  • 4

3 Answers3

1

It will be tough to locate the element through the <span> tag as it is a pseudo element. To locate the element you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.ui-dialog-titlebar-icon.ui-dialog-titlebar-close.ui-corner-all[aria-label='Close']"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all' and @aria-label='Close']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Unfortunately both of these don't work... I get a TimeoutException: Expected condition failed: waiting for element to be clickable – yoopaa Jan 03 '19 at 12:18
  • 1
    @yoopaa _TimeoutException_ is the outcome of **failed** _ExpectedConditions_. Debug your code through `findElement()` inconjunction with `Thread.sleep()`. If you are able to locate the element, update the question with the observations. – undetected Selenium Jan 03 '19 at 12:19
  • I'm still not able to locate the element, updated the question – yoopaa Jan 03 '19 at 13:12
0

As far as I can see in the HTML you have provided:

You can use By.XPATH with this XPATH: "//*[@aria-label='Close']".

Like this:

d.findElement(By.xpath("//*[@aria-label='Close']")).click();

EDIT:

Try using Actions with an Offset this helps if there is an element covering it, this happens with iFrames.

Here is a code snip:

WebElement closePrint = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all' and @aria-label='Close']")));

int width = closePrint.getSize().getWidth();

Actions clicker= new Actions(driver);
act.moveToElement(closePrint).moveByOffset((width/2)-2, 0).click().perform();

Hope this helps you!

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • I get this error: ElementNotInteractableException: Element could not be scrolled into view – yoopaa Jan 03 '19 at 12:20
  • @yoopaa hi, this error has to do with something in your code which you did not share if you can we might be able to help you! – Moshe Slavin Jan 03 '19 at 12:24
  • @yoopaa can you share the url? – Moshe Slavin Jan 03 '19 at 13:40
  • Sadly no, it's a company internal website.. I've now tried it with the javascript executor and I get "element not clickable because another element (iframe id ="innerIframe") obscures it. However, if I switch to that frame, I get another error saying a frame like that doesn't exist? Do you have any idea why? – yoopaa Jan 03 '19 at 13:59
  • I think it has to do with `switchTo().frame`. but can you get the `rect` of this element: `xpath("//a[@class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all' and @aria-label='Close']")`? – Moshe Slavin Jan 03 '19 at 14:01
0

In addition to DebanjanB's suggestion, I would also suggest you to use below JavaScript utility to scroll down till the element is visible.

WebElement element = driver.findElement(By.xpath("//a[@class='ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all' and @aria-label='Close']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Tejas Jagtap
  • 11
  • 1
  • 3