0

I'm having issues clicking on a hidden button his the element in full?

<a class="score-button hidden-xs hidden-sm" data-ux-module="score_bootstrap/Components/Button" data-ux-state="loaded" href="https://testwebsite.com/corporate/careers/jobs">Search all jobs</a>

Here's my test albeit my latest version this time with an xpath which I'm never a massive fan of using anyway. But CSS selector didn't work either.

@And("the user clicks on Search all jobs")
public void the_user_clicks_on() {

    WebDriverWait longWait = new WebDriverWait(driver, Duration.ofSeconds(20));  // Increase the wait time
    WebElement submenuElement = longWait.until(ExpectedConditions.elementToBeClickable(
            By.xpath("//a[contains(@class, 'score-button') and contains(@href, 'corporate/careers/jobs') and contains(text(), 'Search all jobs')]")));
    submenuElement.click();
}

Error I receive:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //a[contains(@class, 'score-button') and contains(@href, 'corporate/careers/jobs') and contains(text(), 'Search all jobs')] (tried for 20 second(s) with 500 milliseconds interval)

The Error is pretty clear it cant find the element but how can the element be found is it nested in to the sub menu?

Am I dealing with a hidden button here or what?

Whats the best and cleanest approach to just click this button?

6
  • Can you share the URL of the page if it is public? Also, what is the error/exception you see on the console? Commented Apr 19, 2024 at 12:14
  • Sure risk.lexisnexis.com/corporate/careers Commented Apr 19, 2024 at 12:19
  • I tried in my local using the same XPath expression, it is working for me. Since its complaining waiting for element to be clickable, can you try ExpectedConditions.visibilityOfElementLocated or this ExpectedConditions.presenceOfElementLocated instead of elementToBeClickable? See if that makes any difference. Commented Apr 19, 2024 at 12:29
  • Thanks for taking a look but neither of those resolved it for me. Weird it runs for you though? Commented Apr 19, 2024 at 12:37
  • Yes I just used your code and it is working, clicking on the desired element. What browser are you launching? Commented Apr 19, 2024 at 12:41

2 Answers 2

0

I used your own code and it is working fine in my machine. Strange that you are seeing org.openqa.selenium.TimeoutException.

Anyway, below is what I tried and it worked:

public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://risk.lexisnexis.com/corporate/careers");
        
    WebDriverWait longWait = new WebDriverWait(driver, Duration.ofSeconds(20));  // Increase the wait time
    WebElement submenuElement = longWait.until(ExpectedConditions.elementToBeClickable(
                By.xpath("//a[contains(@class, 'score-button') and contains(@href, 'corporate/careers/jobs') and contains(text(), 'Search all jobs')]")));
    submenuElement.click();   
}

Since WebDriverWait is not working for you, you can try other options, see below.

  1. Using Actions Class:
import org.openqa.selenium.interactions.Actions;

...

WebElement submenuElement = driver.findElement(By.xpath("//a[contains(@class, 'score-button') and contains(@href, 'corporate/careers/jobs') and contains(text(), 'Search all jobs')]"));
Actions action = new Actions(driver);
action.click(submenuElement).perform();
  1. As a last resort you can use JavaScriptExecutor to click on element as below:

NOTE: JavaScriptExecutor doesn't emulate human actions like selenium's click() methods does. This fails the entire purpose of test automation. One should use JSE only as a last resort when selenium is not able to perform click.

import org.openqa.selenium.JavascriptExecutor;

...

Thread.sleep(5000);
WebElement submenuElement = driver.findElement(By.xpath("//a[contains(@class, 'score-button') and contains(@href, 'corporate/careers/jobs') and contains(text(), 'Search all jobs')]"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", submenuElement);
Sign up to request clarification or add additional context in comments.

6 Comments

What version of Chrome are you on?
I tried on both 123 and 124, working on both.
Would the java version have anything to do with it in intellij?
Not really sure. Did you try the other 2 options? Action Class and Javascript?
Yeah tried all your scenarios none worked on my machine im gonna try my laptop.
|
0

So here's my solution took a while it's similar to the suggestions above but this one has started running correctly I played around with the elements ID until I found the best combination and added the javascript functionality.

    @And("the user clicks on Search all jobs")
    public void the_user_clicks_on()  {
        // Create WebDriverWait instance
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement searchJobsButton = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.score-button[data-ux-module='score_bootstrap/Components/Button']")));

// Scroll the button into view
        JavascriptExecutor executor = (JavascriptExecutor)driver;
        executor.executeScript("arguments[0].scrollIntoView(true);", searchJobsButton);

// Use JavaScript to perform the click action
        executor.executeScript("arguments[0].click();", searchJobsButton);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.