2013-04-14 108 views
0

我有一个使用Selenium浏览“SEARCH”按钮的问题。我需要点击“搜索”按钮,然后回到初始页面并再次点击它。我的代码导航这个按钮,并点击它非常好第一次,然后网页回到初始URL。然后,它应该再次导航相同的按钮,但是,它不工作......我使用了许多不同的方式(xpath等)这里有什么问题?这是我的完整代码。你可以复制粘贴到日食,看看我在说什么:如何使用Selenium WebDriver导航并单击网页上的按钮?

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

    } 
+0

,如果你做了一个明确的等待会发生什么HTTP://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits – jithujose

+0

@jithujose我尝试这样做。它也不起作用。 – Buras

+0

@jithujose这是我厌倦明确的等待的方式。 WebDriverWait wait = new WebDriverWait(driver,60); (@@@@@ = ='Search'] [@ onclick ='return checkinput(this.form,0);'] [@ type ='button “]“))); \t \t driver.findElement( \t \t \t \t By.xpath(“//输入[@值= '搜索'] [@的onclick = '返回checkinput(this.form,0);'] [@类型=”按钮']“)) \t \t \t \t .click(); – Buras

回答

1

任何异常?重定向后DOM是否改变了?您正在使用哪个浏览器?

我注意到在再次转到url之后,按钮变成了<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();

+0

谢谢,我没有注意到:) – Buras

+0

很高兴有它修复。请注意,这只发生在'历史性'搜索中,如果您使用“当前”单选按钮进行搜索,则重定向后按钮的onclick值不会更改。 –

相关问题