java 如何使用 Selenium WebDriver 导航和单击网页上的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16003637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:36:29  来源:igfitidea点击:

How to navigate and click a button on a web page using Selenium WebDriver?

javabuttonselenium

提问by Buras

I have an issue navigating a "SEARCH" button using Selenium. I need to click a "Search" button then come back to the initial page and click it again. My code navigates this button and clicks it perfectly fine the first time, then the web page comes back to the initial URL. Then it supposed to navigate the same button again, however, it does not work...I used many different ways (xpath etc.) What is the problem here? Here is my FULL code. One may copy-paste it to eclipse and see what I am talking about:

我在使用 Selenium 导航“搜索”按钮时遇到问题。我需要单击“搜索”按钮,然后返回初始页面并再次单击它。我的代码在第一次导航这个按钮并完美点击它,然后网页返回到初始 URL。然后它应该再次导航同一个按钮,但是,它不起作用......我使用了许多不同的方式(xpath等)这里有什么问题?这是我的完整代码。人们可以将它复制粘贴到 eclipse 中,看看我在说什么:

     import java.util.concurrent.TimeUnit;
     import org.openqa.selenium.By;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.WebElement;
     import org.openqa.selenium.chrome.ChromeDriver;

     public class Search {

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

    System.setProperty("webdriver.chrome.driver",
            "chromedriver\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

    // Getting the initial page
    driver.get("http://pqasb.pqarchiver.com/chicagotribune/advancedsearch.html");
    driver.findElement(By.xpath("//input[@value='historic']")).click();
    WebElement element = driver.findElement(By.name("QryTxt"));
    element.sendKeys("issue");
    driver.findElement(
            By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 0);'][@type='button']"))
            .click();

    // Getting back to the initial page
    driver.get("http://pqasb.pqarchiver.com/chicagotribune/advancedsearch.html");
    driver.findElement(
            By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 0);'][@type='button']"))
            .click();
    /**
     * This command does not execute. It is supposed to click on the button
     * "SEARCH" It worked well in the above identical code line, however now
     * it just does not recognize the existence of this button How can I
     * overcome this issue? I tried navigating this button by all different
     * means (xpath etc...)
     */
}

     }

回答by Yi Zeng

Any exceptions? Did DOM change after redirect? Which browser you are using?

有什么例外吗?重定向后 DOM 是否发生了变化?您使用的是哪个浏览器?

I noticed that button changed to <input type="button" onclick="return checkinput(this.form, 1);" value="Search"/>after the go to the url again.

我注意到按钮<input type="button" onclick="return checkinput(this.form, 1);" value="Search"/>在再次转到 url 后更改为。

so you need driver.findElement( By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 1);'][@type='button']")).click();

所以你需要 driver.findElement( By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 1);'][@type='button']")).click();