单击 selenium java 中的隐藏按钮

问题描述 投票:0回答:1

我在单击完整元素的隐藏按钮时遇到问题?

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

这是我的测试,尽管这次是我的最新版本,使用的是 xpath,无论如何我从来都不是使用它的忠实粉丝。但 CSS 选择器也不起作用。

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

我收到的错误:

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)

错误很明显,它找不到该元素,但是如何找到该元素是否嵌套在子菜单中?

我在这里处理的是隐藏按钮还是什么?

单击此按钮的最佳和最干净的方法是什么?

java selenium-webdriver
1个回答
0
投票

我使用了你自己的代码,它在我的机器上运行良好。奇怪的是你看到的是

org.openqa.selenium.TimeoutException

无论如何,以下是我尝试过的方法并且有效:

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

由于

WebDriverWait
不适合您,您可以尝试其他选项,请参阅下文。

© www.soinside.com 2019 - 2024. All rights reserved.