0

I have a dropdown selection, the class changes based on the mouse movement on the dropdown values (Active value)F12 HTML screenshot with the UI

I tried the below code and it worked for 'Individual'

driver.findElement(By.cssSelector("div[class=ui-menu-item-wrapper]")).click();

If you see the screenshot, Class 'ui-menu-item-wrapper' is same for both Individual and Corporation.

Question: I am not sure how to select Individual or Corporation using cssSelector. I want to give Individual or Corporation from the excel data feed.

5 Answers 5

1

Here is the option to select using xpath.

Xpath:

//ul[starts-with(@class,'ui-menu') and @role='combobox']//div[normalize-space(.)='" + valueFromExcel +"']

//ul[starts-with(@class,'ui-menu') and @role='combobox']//div[normalize-space(.)='Individual']

Using xpath is simple and direct in this case as you are planning to use the text to identify the element which is not supported by CSS.

Still if you want to stick to the CSS then you have to get each element text using css and then select nth element if text match.

Sign up to request clarification or add additional context in comments.

1 Comment

Let me know the language if you are looking for CSS solution.
1

Use WebDriverWait to handle dynamic element and following Xapth to click on the element.Hope this code will work for you.

String text="corporation";
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ui-menu-item-wrapper' and text()='" + text + "']"))).click();

3 Comments

I tried the above and got this error Expected condition failed: waiting for element to be clickable: By.xpath: //div[@class='ui-menu-item-wrapper' and text()='corporation'] (tried for 30 second(s) with 500 milliseconds interval)
instead of image can you share html text it very difficult provide answer based on snapshot
Thanks for your Answer, I got the resolution from another answer
1

If it is not necessary for some reason to use the CSS selector and the ID of the elements are not dynamic and stays the same, you can find the elements by ID. Or you can find the elements by text using the XPath.

Here are some of the ways you can locate the second element of the list.

ID

driver.findElement(By.id("ui-id-194")).click();

XPath

//Finds the element by text
String text = "Corporation";
driver.findElement(By.xpath("//li[@class='ui-menu-item']/div[contains(text(),'"+ text +"')]")).click();

CSS Selector

//Finds the second div with the same class
driver.findElement(By.cssSelector("div[class=ui-menu-item-wrapper]:nth-of-type(2)")).click();

Also, you can use the CSS selector or xpath to get the element using it's id as shown in Valga's answer.

3 Comments

ID - id is dynamic so cannot use this one, next login id changes. XPath worked - Thank you, I am wondering how to pass this value now from external source.
concatenate the string in the XPath. I've edited the xpath code so that you can use the text variable to change the xpath.
Actually, Another answer above by Valga fixed my issue, I used your answer and it was helpful as well. I up voted your answer. Thank you again.
0

You can find matching elements with a selector that finds the prefix "ui-menu-item-wrapper":

driver.findElements(By.cssSelector("div[class^=ui-menu-item-wrapper]"));

3 Comments

Hi Andrew, I tried the same and it worked for Individual, But I want to select Corporation and May be want to give them as a data feed from Excel
@gkiranreddy I just edited the answer, I believe you have to use findElements instead of findElement
I am a Novice in Selenium, How should I select Individual or Corporation after FindElements. FindElements.dot gives 'contains' 'equals'. and I want to click on the value.
-1

Try these

Using the ID of the Corporation DIV

driver.findElement(By.cssSelector("[id='ui-id-194']")).click();

or use findelements to get a list of elements that contains in their class "ui-menu-item-wrapper", then click on the second item of the list (0 is first item, 1 is second, in this case, corporation.)

driver.findElements(By.cssSelector("div[class*=ui-menu-item-wrapper]")).get(1).click();

you can also do this to click on an element of the list that contains certain text:

for (WebElement x : driver.findElements(By.cssSelector("div[class*=ui-menu-item-wrapper]"))){
    if (x.getText().equals("StringValueYouWantHere")){
        x.click();
        break;
    }
}

Hope it helps.

3 Comments

ID does not work in my case as it changes every login, I think its dynamic value. Your second option gave this error and not sure how to handle this The type of the expression must be an array type but it resolved to List<WebElement>
Fixed it, i forgot that java cant handle lists like that
Thanks a lot, the below code worked perfectly String clienttype = ExcelUtils.getCellData(16,2); for (WebElement x : driver.findElements(By.cssSelector("div[class*=ui-menu-item-wrapper]"))){ if (x.getText().equals(clienttype)){ x.click(); break; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.