Home > OS >  Dropdown selector in selenium Automation
Dropdown selector in selenium Automation

Time:11-30

Hi I did a script to login github.com and dropdown then I need to select sign out option from drop down. I tried with class name and xpath both were not working.

Code for select drop down menu.

driver.findElement(By.xpath("/html/body/div[1]/header/div[7]/details/summary/span[2]")).click();

Code for selecting signout option in dropdown menu

driver.findElement(By.xpath("/html/body/div[1]/header/div[7]/details/summary/span[2]")).click();

Till dropdown menu it was working later selecting in drop down menu was not working.

CodePudding user response:

driver.findElement(By.xpath("/html/body/div[1]/header/div[7]/details/summary/span[2]")).click();

The element you are referring is not belongs to Sign out button. Please use any one of the elements.

driver.findElement(By.xPath("//button[@class='dropdown-item dropdown-signout']")).click();

or

driver.findElement(By.cssSelector("button[class*='dropdown-item dropdown-signout'")).click();

The class name of Sign out button is compound class name so you cannot directly use with By.className. Compound class names are not permitted in selenium if you use then you will experience the CompoundClassException

CodePudding user response:

instead of using class names you can use specific text such as "Sign out". then your code for the clicking sign out button could be like :

driver.findElement(By.xPath("//button[contains(text(),'Sign out')]")).click();
  • Related