0

我在使用 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...)
     */
}

     }
4

1 回答 1

1

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

我注意到该按钮<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();

于 2013-04-14T21:14:51.440 回答