Thursday, May 16, 2019

Finding element ByChained using chained class


DOM Looks like this for Google Search button





 package secondpackage;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.support.pagefactory.ByChained;  
 public class DimensionTest {  
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver",  
                     "C:\\Users\\Mitturaj.h\\Desktop\\chromedriver_win32\\chromedriver.exe");  
           WebDriver driver = new ChromeDriver();  
           driver.navigate().to("https://www.google.com/");  
           driver.manage().window().maximize();  
           driver.findElement(By.name("q")).sendKeys("Karnataka");  
           WebElement element = driver  
                     .findElement(new ByChained(By.xpath("//div[@class='FPdoLc VlcLAe']"), By.name("btnK")));  
           element.click();  
      }  
 }  

Other ways is put in the List and click on the second element

 List<WebElement> searchElements = driver.findElements(By.name("btnK"));  

No comments:

Post a Comment