yoopaa
yoopaa

Reputation: 29

How to find element either by "a class" or "span class"?

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!

Upvotes: 1

Views: 141

Answers (3)

Tejas Jagtap
Tejas Jagtap

Reputation: 11

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);

Upvotes: 0

Moshe Slavin
Moshe Slavin

Reputation: 5204

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!

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

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();
    

Upvotes: 1

Related Questions