如何使用Selenium WebDriver导航并单击网页上的按钮? [英] How to navigate and click a button on a web page using Selenium WebDriver?

查看:95
本文介绍了如何使用Selenium WebDriver导航并单击网页上的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Selenium导航"SEARCH"按钮时遇到问题.我需要单击搜索"按钮,然后返回到初始页面,然后再次单击它.我的代码会导航此按钮,并在第一次 上单击它就可以了,然后网页返回到初始URL.然后它应该再次导航相同的按钮,但是,它不起作用...我使用了许多不同的方式(xpath等).这是什么问题?这是我的完整代码.可以将其复制粘贴以使其黯然失色,看看我在说什么:

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

     }

推荐答案

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

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

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

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

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

这篇关于如何使用Selenium WebDriver导航并单击网页上的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆