0

I'm working on a test automation case and I have the following problem.

On a web form I have a dropdown menu, with several items on them. I need to select one of them but only know the content which I am expecting.

From the following images I want to select 'PostgreSQL' as my option.

UI part:

enter image description here

HTML part:

enter image description here

This is the code I am using to open the dropdown:

var SourceSystem  = driver.FindElement(By.XPath("/html/body/qe-config-page/div/div/div/div/div[1]/div[17]/div/div/div/div[2]/div/div/div/div/div/div/div[3]/div/div[2]/div[1]/div/div")); 

SourceSystem.Click();

For the item selection on the list I've tried FindingElement by XPath which didn't work and SelectElement by Text which also found nothing.

Changes on the UI are not applicable in my case.

First Solution tested:

var SourceSystem  = driver.FindElement(By.XPath("/html/body/qe-config-page/div/div/div/div/div[1]/div[17]/div/div/div/div[2]/div/div/div/div/div/div/div[3]/div/div[2]/div[1]/div/div")); 
    
SourceSystem.Click();
var select = new SelectElement(SourceSystem);
select.SelectByText("PostgreSQL");

Result: Element should have been select but was div

Pasterz
  • 11
  • 1
  • 6

1 Answers1

0

First and foremost: stop using 'Copy Xpath' in your browser. Just stop. Don't ever use it. It is wrong, it is evil, it will break, but the worst quality of 'Copy Xpath' is that it tricks you into thinking that you don't need to learn how Xpath works.

But you do. And it's very easy once you understand it. Here's a great primer I'd recommend you read: https://www.toolsqa.com/selenium-webdriver/write-effective-xpaths/

Now, looking at your DOM, the Xpath you can use to find the dropdown is:

//div[contains(@class, "vui-popup")]

The Xpath to find the option you want to click is:

//div[contains(text(), "PostgreSQL")]