简体   繁体   中英

I am having trouble in locating search bus button in red bus site using selenium webdriver with java

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","./drivers/chromedriver");
    WebDriver driver=new ChromeDriver();
    driver.get("https://www.redbus.in/");
    driver.findElement(By.id("search_btn")).click();
}

Html code:[ https://i.stack.imgur.com/9mdsv.png][https://i.stack.imgur.com/ODjxk.png]

I am not able to click the search bus button.

As per the HTML you have shared to locate the Search Buses button and invoke click() you can use either of the following line of code :

  • cssSelector

     driver.findElement(By.cssSelector("button.fl.button#search_btn")).click();
  • xpath

     driver.findElement(By.xpath("//button[@class='fl button' and @id='search_btn']")).click();

Update

With Selenium-Java Client v3.9.1 , GeckoDriver v0.19.1 and Firefox Quantum v58.0.2 this block of code works perfect at my end :

System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver =  new FirefoxDriver();
driver.get("https://www.redbus.in/");
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.fl.button#search_btn"))).click();
System.out.println("Search button clicked");
driver.quit();

Console Output :

Search button clicked

Snapshot :

搜索按钮被点击

you can try this xpath:

driver.findElement(By.xpath("//section[@id='search']
//button[@id='search_btn']")).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