How to click on select options using selenium - java

I can't click on option element-
website: http://www.oferty.net
When I am trying to select one option in the drop-down list. Unfortunately
I receive the message:
"Cannot click on option element. Executing JavaScript click function
returned an unexpected error"
What did I do wrong with the following code:
Select estateType = new Select(driver.findElement(By.id("ps_type")));
estateType.selectByVisibleText("domy");
Please help.

I quicky check the HTML code of the dropdown you are trying to select and it's not a HTML dropdown. it's custom dropdpown created with CSS and so you can't use the default function provided by Selenium to select Dropdown Value.
<div class="jquery-selectbox-list jquery-custom-selectboxes-replaced-list" style="width: 113px; height: 9em; display: none;">
<span class="jquery-selectbox-item value-998 item-0">rynek pierwotny
</span>
...
</div>
Possible Solutions
Try SendKeys on jquery-custom-selectboxes-replaced-list element and see if it's working, if not
Click on jquery-custom-selectboxes-replaced-list and then scroll to the element you are interested in and click on it.

It's a simulated dropdown list, not native. Selenium Select Class can only work on native dropdown list define by Select Tag. Even there is also embed a native dropdown, but it's hidden, that's why report can't click on option error. And When you operate on the dropdown from page, the embed native dropdown is always hidden, thus actually you not operated on the embed native one.
Try below code:
public void choosePsType(String psType) {
// find the container node of the dropdown list
WebElement psTypeSelect = driver.findElement(By.cssSelector("label[for='ps_type'] + div"));
// click on the selector to expand options
psTypeSelect.findElement(By.cssSelector("span.jquery-selectbox-currentItem")).click();
// choose wanted option
String xpath = String.format(".//span[contains(#class, 'jquery-selectbox-item')][.='%s']", psType);
psTypeSelect.findElement(By.xpath(xpath)).click();
}

Related

Selenium Webdriver with Java - Click on an hyperlinked Dropdown Container

I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.

How to select an item from the dropdown list through selenium webdriver and java

Update:
I want to be able to click on the options available example in the HTML code. There are two options (id="all_setup_home" and id="developer-console-link"). Currently, the XPath I am utilizing clicks randomly on the dropdown and it takes me to the option 1 page (that's what i want) but it is not very dynamic as my XPath does not target option 1 or 2 but the drop-down. And so if I want to click on the second option I will not be able to.
Anything better than this would be much appreciated.
The workaround that's working for now:
getElementByXPath("Settings").click();
Thread.sleep(3000);
driver.findElement(By.xpath("//ul[contains(#class,'scrollable')]")).click();
Inital work that is still not working:
<!-- begin snippet: js hide: false console: true babel: false -->
HTML
<div class="popupTargetContainer menu--nubbin-top uiPopupTarget uimenuList uiMenuList--right uimenuList--default visible positioned" data-aura-rendered-by="101:185;a" data-aura-class="uiPopupTarget uimenuList uimenuList--right uimenuList--default" aria-labelledby="59:185;a">
::before
<div role="menu" data-aura-rendered-by="95:184;a">
<!--render facet:96:184;a-->
<ul class="scrollable" role="presentation" data-aura-rendered-by="97:184;a">
<!--render facet: 816:0-->
<!--render facet: 882:0-->
<li class="slds-dropdown__item uiMenuItem onesetupSetupMenuItem" role="presentation" id="all_setup_home" data-aura-rendered-by="893:0" data-aura-class="uiMenuItem onesetupSetupMenuItem">....</li>
<!--render facet:826:0-->
<!--render facet:2004:0-->
<li class="slds-dropdown__item uiMenuItem onesetupSetupMenuItem" role="presentation" id="developer-console-link" data-aura-rendered-by="893:0" data-aura-class="uiMenuItem onesetupSetupMenuItem">....</li>
<!--render facet:826:0-->
<!--render facet:2004:0-->
I had a similar issue, I don't use selenium so I cannot write the code for you but after your dropdown.click(); you have to wait a little moment, cause at the moment you do not wait for the list to be open.
As per your comment as you are able to open the dropdown next as per the HTML you have shared to select an item from the dropdown you have to induce WebDriverWait for the items to render within the HTML DOM and you can use the following code block :
String value = "all-setup_home";
WebElement dropdown = driver.findElement(By.xpath("//span[#class='slds-icon_container slds-icon-utility-setup slds-button__icon slds-global-header__icon']"));
dropdown.click();
List<WebElement> options = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div[#class='popupTargetContainer menu--nubbin-top uiPopupTarget uimenuList uiMenuList--right uimenuList--default visible positioned']/div[#role='menu']/ul[#class='scrollable']//li[#class='slds-dropdown__item uiMenuItem onesetupSetupMenuItem']")));
for (WebElement option : options)
{
if (option.getAttribute("innerHTML").equals(value))
{
option.click();
break;
}
}
So, I was able to click on the first option getting the XPath from this element:
driver.findElement(By.xpath("//ul[contains(#class,'scrollable')]")).click();
However, this isn't very dynamic as I am unable to click on the second option.

How to select an item from a CSS custom dropdown list using Selenium WebDriver with java?

I want to select an item in a CSS dropdown but there seems to be no way to do it, we can take as an example the google hotel review page here how can I select by most recent reviews programmatically through selenium ?
Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically.
I've tried this way :
Select select = new Select(driver.findElement(By.xpath("//*[#id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
select.deselectByIndex(1);
but I'm getting an exception(org.openqa.selenium.support.ui.UnexpectedTagNameException) saying :
Element should have been "select" but was "g-dropdown-menu"
Is there a way to simulate a click on a CSS dropdown element like this with Selenium web driver ?
Analysis:
Selenium Java API: Select.class only suitable for dropdown which use HTML select tag. For dropdown implment by other way, like JQuery dropdown plugin, Select class not support, for such dropdown you need to click on the drop down to make the options to display out, then choose the option you wanted.
Solution:
public void selectSortby(String sortBy) {
// click on dropdown to expand options
driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
// choose option
driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).click();
}

how to find xpath in selenium webdriver

Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button.
<regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>
If you are using Google Chrome, right click on the button, and select Inspect in the popup. The html will open in a developer tools frame. Right click on the element in the developer tools frame, hover over copy, select copy xpath.
Here is the XPath to the form on the URL.
//*[#id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button
WebDriver driver = new ChromeDriver();
driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
//fill in fields
WebElement element = driver.findElement(By.xpath("//*[#id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
element.click();
driver.findElement(By.xpath("//regform-button/button")).click();
Try opening the console for Google Chrome and typing the following:
document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()
It works...that's if you've specified the required fields. Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields.
Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. I'm saying that "//regform-button/button" should work..
Once you have selected the required values from the drop-downs appearing before Continue button, you can click on Continue button using the following statement:
driver.findElement(By.xpath("//span[text()='Continue']")).click();
The following xpath worked for me.But I dont know, why is it different for each different browser.
For Firefox :
`driver.findElement(By.xpath("//*[#id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;
For chrome: driver.findElement(By.xpath("//regform-button/button")).click();

findElement by class where content equals text

I'm trying to locate and click an element on my page but can't use the by.id method as the id's are generated and change per session. For most elements I can get around this by using xpath but there is a dropdown menu where this does not work. I can click the element containing the dropdown and it shows me the options. If I locate the element I need and copy it's xpath the test case won't function stating it can't find the xpath. Now next to the id the Element I'm trying to click also has a class. Problem is that this class is not unique, all menu items in the dropdown have the same class with a different text. What I would like to do is something like:
driver.findeElement(By.class("x-menu-item-text").equals("Unique text 1here").click()
The class "x-menu-item-text" is not unique but the text in this particular class is. I can't use the ID as this is automatically generated. The full code for the item or element I want to click is:
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 1 here</span></a>
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 2 here</span></a>
I'm using Selenium Webdriver with Eclipse (Java).
Allthough the answer provided seems to work on most pages and locations, there is a situation however where I can't get it to work. Can anyone advise?
There is a page with buttons and I want to click one of these buttons. If I use the following statement:
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Add']")).click();
I get an error message
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
If I look at the source I see:
<button class=" x-btn-text" id="ext-gen539" type="button">Add</button>
So the element is present and visible.
I've tried adding a wait.until statement before the click statement but this does not work either:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")));
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")).click();
Extra information: could this problem be because I'm looking for an element that is located in a popup?
You can use xpath locator
https://newcircle.com/bookshelf/selenium_tutorial/locators
By.xpath("//span[#class='x-menu-item-text' and text()='Unique text 1here']")

Categories

Resources