如何使用 selenium 单击引导程序按钮

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

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

<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 bootstrapper
2个回答
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
不适合您,您可以尝试其他选项,请参阅下文。


0
投票

所以这是我的解决方案,花了一段时间,它与上面的建议类似,但这个解决方案已经开始正确运行,我尝试了元素 ID,直到找到最佳组合并添加了 javascript 功能。

    @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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.