Selenium Java- Drop down selection where Style- display is none - java

When I try to select a drop down with Style display: none;
option 1:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");
The above code renders error:
element not interactable: Element is not currently visible and may not
be manipulated
Option 2:
WebElement hiddenWebElement =driver.findElement(By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);
Option2 recognizes the drop down but unable to select an item from the drop down.
Any help would be appreciated.
Tried few options i see in the site but didtn't help much

You could try altering the style attribute using Javascript, as such:
hiddenWebElement = driver.findElement(
By.xpath("//select[#name='ctl00$ContentPlaceHolder1$ddlFeedStatus']"));
((JavascriptExecutor)driver).executeScript(
"arguments[0].style.display = 'block';", hiddenWebElement);
After that, you can try a Javascript click or regular click.

Could you right click -> Inspect and see if it is really a dropdown? If drop down is visible and still getting this error, it might not an html dropdown. It may look like one but can only confirm if you inspect or look at code.
If element is an html drop down, use the following code to see if it works:
WebElement sysDropDown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddlFeedStatus"));
Coordinates coordinate = ((Locatable) sysDropDown).getCoordinates();
coordinate.onPage();
coordinate.inViewPort();
Select sDropdown = new Select(sysDropDown);
sDropdown.selectByVisibleText("01 - Quarantined");

Related

Not able to identify the element in dropdown

enter image description hereenter image description hereI have a drop down, and click will display the list of branches.
I am able to identify the drop down(arrow inverted in ui) and click on it, by using below code.
//click on the drop down
#FindBy(xpath ="//[#id=\"miniTable\"]/tbody/tr[5]/td[1]/div/div/div[1]")
WebElement selectbranch;
Note that ,There is no select tag for the drop down
Issue:
I am able to identify the drop down and click on that, But I am not able to get one of the branch from the drop down.
Since your Drop down has not made of Select tag, Select class from selenium will not work.
As you have mentioned that you are able to click on drop down, you can use this code after that :
List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
if(element.getText().equals(" the value you want to select from drop down")){
element.click();
}
}
in place of your locator , you'd have to give a common locator for all the elements of drop down.
Let me know if you have any more concerns.

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.

Drop down button is not responding with selenium in java

I want to click on drop down button and select one among one and click on it.I tried by many ways but it is not working. Attached image of html code, Anyone help me regarding this.
Below is my code which i tried
driver.findElement(By.id("homepages_dropdown")).click();
/*List<WebElement> allElements = driver.findElements(By.xpath("//*[#id=\"homepages_item_AccountManagerDashboard\"]"));
for (WebElement element: allElements)
{
System.out.println(element.getText());
}*/
Just use "Select" to create an object that handles dropdowns etc. You can use "Select" to select element by it's text, index and a bunch of other options. It's the easiest way to handle such elements as dropdown and comboboxes.
new Select(driver.findElement(By.id("homepages_dropdown"))).selectByVisibleText("Your option's text");
This isn't the sharpest way to do it, but if you can't get developers to make it a specific select element via HTML it may have to do. I have stuff like this in my solution where there are custom dropdowns.
You can start by creating a dropdown element and then click a single record in it if that's what you're trying to do, in your case it looks like it is the span within the highlighted button element, so you can do something like the following:
driver.findElement(By.id("homepages_dropdown")).click();
driver.findElement(By.xpath("//li//a[#id='homepages_item_AccountManagerDashboard']").click();
The above statements will click the dropdown then click the element with the id of homepages_item_AccountManagerDashboard.
You can also create a function that clicks any li of based off of a parameter you pass into a method you create, consider this if you may select many options in your dropdown.
public void SelectItem(string itemText)
{
var dropdownElement = driver.findElement(By.id("homepages_dropdown"));
var selectionItem = driver.findElement(By.xpath("//li[text()='" + itemText + "']");
dropdownElement.Click();
selectionItem.Click();
}
This is only really a valid option if you are working through the Page Object Model, but it's the best way to make things work consistently long term.
To click on Dropdown button and Select the Option with text as Analytics you can use the following code block :
driver.findElement(By.xpath("//div[#id='homepages']/button/span[#id='homepages_dropdown']")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//div[#id='homepages']/ul/li/a"));
for (WebElement element : allElements)
if(element.getAttribute("innerHTML").contains("Analytics"))
{
element.click();
break;
}
System.out.println("Option with text as Analytics is selected");
I found solution myself,Below code worked
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("homepages_dropdown")));
// drop down
driver.findElement(By.id("homepages_dropdown")).click();
Thread.sleep(2000);
// selecting configuration
driver.findElement(By.id("homepages_item_ConfigurationHomepage")).click();
Thread.sleep(2000);

WebDriver -- Selenium I can not select the dropdown option

I can not understand what the problem is, tried different ways of choosing, I give examples below...
1. You must select or click a selection.
2. Select or click the desired language.
But WebDriver does not see, does not find these elements, but in DOM they are.
You need to go to LinkedIn profile, settings, language selection.
//Before select dropdown.
WebElement language = driver.findElement(By.id("setting-select-language"));
language.click();
//Select dropdown.
Select make = new Select(driver.findElement(By.name("selectLanguage")));
make.selectByValue("en_US");
//Or
Actions act = new Actions(driver);
//XPath of dropdown.
act.moveToElement(driver.findElement(By.name("selectLanguage"))).click().perform();
//XPath of option in the dropdown.
act.moveToElement(driver.findElement(By.xpath("//*[#id='setting-select-language-content']/form/div/select/option[16]"))).click().perform();
focus = true focus = false
When you click on select it changes focus = true
I believe you locator for the select is incorrect, try something like the below code:
WebElement language = driver.findElement(By.id("setting-select-language"));
language.click();
Select make = new Select(driver.findElement(By.name("//*[#id='setting-select-language-content']/form/div/select")));
make.selectByValue("en_US");

How can i select text from browser screen with help of mouse events in selenium?

I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();

Categories

Resources