0

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

     }
6
  • What happens if you do an explicit wait?docs.seleniumhq.org/docs/… Commented Apr 14, 2013 at 20:07
  • @jithujose I tried this. It does not work either. Commented Apr 14, 2013 at 20:14
  • @jithujose Here is the way I tired explicit wait. WebDriverWait wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 0);'][@type='button']"))); driver.findElement( By.xpath("//input[@value='Search'][@onclick='return checkinput(this.form, 0);'][@type='button']")) .click(); Commented Apr 14, 2013 at 20:15
  • What does it exactly mean "does not execute"? BTW, there are two buttons which match the xpath. Also, you are not filling any search options, it stops submitting the form. Commented Apr 14, 2013 at 20:50
  • @kan By "does not execute" I mean that the button is NOT clicked at all. Even if I do not fill the search options it MUST be clicked. You may try adding additional terms or filling out the form, it still does not recognize this button. Also, as you mentioned there are two buttons that match the xpath, however, in this case Selenium must choose the fisrt one. in addition the button was clicked firtst time (there were two elements with identical xpathes) Commented Apr 14, 2013 at 21:01

1 Answer 1

1

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"/> 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();

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I didn't notice that :)
Glad to have it fixed. Note that this only happens to 'historic' search, if you search using current radio button, button's onclick value won't change after redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.