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.
Related
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();
}
I want to perform click action on a button present under .svg layout using Selenium (Java bindings).
For example, I want to click on the button element, but every time I try to find an element by xpath, I get exception `enable to locate element
I read that with Selenium its tricky to click on element present under the .svg.
Is there anybody who knows a solution because, I haven't found a suitable solution on the net by myself.
Find below HTML code look likes this way:
My code:
List <WebElement> frame1=driver.findElements(By.xpath("//iframe[contains(#id,'-06636000002Pb2L')]"));
System.out.println(frame1.size());
System.out.println(frame1.get(0).getAttribute("title"));
driver.switchTo().frame(0);
ElementaryOperations.Sleep(3000);
System.out.println("New relation frame found");
driver.findElement(By.cssSelector("#newrel")).click();
After switching to frame successfully, I am not able to click on the element present under the SVG layout. Please refer to the attached screen shot(link)
this is how you should be able to identify SVG node and then sub nodes of that.
//*[local-name() = 'svg']
use this code
driver.findElement(By.id("rfb")).click();
try using class attribute of the button using CSS Selectors:
driver.findElement(By.cssSelector(".btn.btn-primary")).click()
There is a sort option on the website which upon clicking presents options which i want to click on.
Using the click() event for that sort label, does nothing.
That highlighted part is the label which is clickable in image, though click event for the same doesn't work via script.
From that list, i want to click on Most listings option.
Code:
WebElement op= driver.findElement(By.id("sortBy-label"));
op.click();
I am not sure why it is not working without looking into the application. I can come up with something with the shared HTML. Make sure you use explicit waits and wait for the search results to be displayed before clicking on the SORT OPTIONS. Why don't you try this?
//wait for the search results. Use explicit waits and try this
driver.findElement(By.linkText("SORT BY: MOST ACTIVE")).click();
List<WebElement> lis=driver.findElement(By.id("sortBy-menu")).findElements(By.tagName("li"));
for (WebElement li : lis) {
if (li.getText().trim().equals("Most listings")) {
li.click();
}
}
I am working with a HTML select (i.e. dropdown) in a web page that initially loads with no value. It loads the values only when an onfocus JS event is executed on it. Note that it is a GWT control.
Here is the DOM rendering of the dropdown. Note the onfocus event that actually loads the values in the dropdown:
<select id="Field1" class="select not_disabled" onchange="clearOperators(Operator1); addOperators(Field1,Operator1); adjustInput(Field1, 1);" onfocus="loadValues(Field1);" style="width:20em;" name="Field1">
</select>
The problem is that the click on the element is not getting registered unless the window is in focus. And hence the values are not loading.
I have this in my code, and this executes with no effect:
// bring the focus on the dropdown
((JavascriptExecutor) webDriver).executeScript("return document.getElementById('Field1').focus;", dropdownElement);
// click on it to load the values
dropdownElement.click();
Even tried the old way of firing an event on the element. Did not work:
JavascriptLibrary javascript = new JavascriptLibrary();
javascript.callEmbeddedSelenium(webDriver, "triggerEvent", dropdownElement, "blur");
I also tried out some of these suggestions, but none helped:
Correct way to focus an element in Selenium WebDriver using Java
https://groups.google.com/forum/#!msg/selenium-users/jk59XG2TQk8/xbaskp9uFnQJ
Could someone suggest what I can do to load the values in the dropdown? I am using Java with Selenium 2 on Firefox 47.
I found a way to get this working. Although it's not a very desirable solution for UI tests, since it does not mimic a "real user" behavior, it does work fine.
I am invoking the onfocus function from within my test class and that does the loading of values.
(String) ((JavascriptExecutor) webDriver).executeScript("return loadValues(Field1);", dropdownElement);
Further, I added this preference to the Firefox profile:
profile.setPreference("focusmanager.testmode",true);
Hope it helps someone.
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();