簡體   English   中英

如何使用Selenium WebDriver導航並單擊網頁上的按鈕?

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

我在使用Selenium導航“ SEARCH”按鈕時遇到問題。 我需要單擊“搜索”按鈕,然后返回到初始頁面,然后再次單擊它。 我的代碼在第一次瀏覽此按鈕時就單擊它,然后將其完美地單擊,然后網頁返回到初始URL。 然后它應該再次導航相同的按鈕,但是,它不起作用...我使用了許多不同的方式(xpath等)。這是什么問題? 這是我的完整代碼。 一個人可以將其復制粘貼以使其黯然失色,看看我在說什么:

     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...)
     */
}

     }

有什么例外嗎? 重定向后DOM是否發生了變化? 您正在使用哪個瀏覽器?

我注意到按鈕已更改為<input type="button" onclick="return checkinput(this.form, 1);" value="Search"/> 再次轉到網址后,返回<input type="button" onclick="return checkinput(this.form, 1);" value="Search"/>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM