Home > Net >  Selenium webdriver (java) - Click on desired link of some search results
Selenium webdriver (java) - Click on desired link of some search results

Time:08-26

I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.

Code :

 WebElement search = driver.findElement(By.xpath("//*[@id=\"search_keyword\"]")); 
search.sendKeys("59825"); 
search.sendKeys(Keys.ENTER); 
driver.findElement(By.partialLinkText("Cashew")).click(); 
------My 1st try------------- 
//WebElement link = driver.findElement(By.xpath("\"//div[@id = 'Links']/a[3]\"")); 
//link.click();

 ------My 2nd try------------- 

//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]")); 
//find.get(1).click(); 
} 
} –

CodePudding user response:

You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results. Then click on whatever index you want to click.

I am not using an IDE for this but it would look something like this:

driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();

CodePudding user response:

As said, you can try with css ".product-block.inner-product-block" Then

  1. get List of WebElements
  2. do loop
  3. inside loop, try get text of each element or innerText attribute
  4. cross check if it is required one or not by simple if condition
  5. If so, click on that web element and break loop

if this locator is not giving required info, try other locator. say $$("a h3") for veg names.

  • Related