我有一个使用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...)
*/
}
}
,如果你做了一个明确的等待会发生什么HTTP://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits – jithujose
@jithujose我尝试这样做。它也不起作用。 – Buras
@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