简体   繁体   中英

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

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:

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

     }

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM