Java Selenium Get Element for Click - java

i have some problems to get the right element to perform a click.
I use selenium.
I want to click on the Log In "Button" on this page https://campus.uni-stuttgart.de/cusonline/webnav.ini
Maybe sb could help me.
Thanks

Try this code. The login button is available under a frame. so it is necessary to switch to that frame in order to access the element.
driver.get("https://campus.uni-stuttgart.de/cusonline/webnav.ini");
driver.switchTo().frame(driver.findElement(By.xpath("//*[#name='menue']")));
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).click();
driver.switchTo().defaultContent();

It appear the login button id is "menue_frame_key_icon".
So this should be:
driver.findElement(By.id("menue_frame_key_icon")).click();
You could alternately try the child element of that id, which can be done multiple ways. This will work though:
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).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.

I want to perform click action on button present under "svg" layout using selenium library

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()

How to Handle Pop Up IFrame Window In Selenium

I am new in Slenium.
I am trying to handle the pop-up Form.
When I click in New Button the pop-up form like this will open.
I try the handle this by alert(), pop up handling and also by Child Browser Handling. But didn't get the solution.
Please suggest some solution for this issue
If it's an <iframe> element then you need to switch WebDriver to this frame in order to work with it. Here's an example of how you can do this:
By locIframe = By.xpath("//iframe[#name='popup']");
driver.switchTo().frame(driver.findElement(locIframe));
// driver is an instance of RemoteWebDriver
The Xpath locator is just an example: you need to write your own here. Also you can use any other locator to find that <iframe> element in the page source.
After switching to iframe element WebDriver will see it's page source and will be able to work with it.
I also faced the similar issue. When clicked on EDIT Button in the parent window, an iFrame pop-up appears as shown below:`
As mentioned that you tried to handle using alert() or pop up handling and also by Child Browser Handling. But none of them will work since it is an iFrame pop-up. So you need to switch WebDriver to this frame in order to work with it.
For my case I did using the following code:
driver.switchTo().frame(3);
To find the index of the frame you can use Selenium IDE.

Click event on sort option doesn't work: Selenium Webdriver

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();
}
}

Issue with focus in ie browser using selenium webdriver code

Firstly i need to mouse over and then it opens a drop down then i will click on that link.Its working fine in firefox,chrome and issue is in ie.
Here is the code
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act1=new Actions(driver);
//act1.click(element2);
act1.moveToElement(element2).click(element2).build().perform();
It works even in IE browser, when we use this piece of code
caps.setCapability("requireWindowFocus", true);
If we use requiredwindow focus its working fine even in IE browser so no issues with locators
But am not encouraged to use above requiredwindowfoucs code in my project.
Is there any other way to do it.
The issue in Ie browser when we are not using requiredwindowfocus, it is clicking on some other link, so am assuming the problem is with focus.
so kindly help me with this issue without using requiredfoucswindow
I have has similar issues in my project. In IE, clicking drop down values does not work. I have a pretty weird and unexpected solution for this. Find the element before performing the operations. I am assuming that this is required because when you move to element1 and when trying to find element the IE Xpath processor messes up the focus.
WebElement element=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/a"));
WebElement element2=driver.findElement(By.xpath("/html/body/div/span/form[2]/div[1]/div[1]/div[3]/div[2]/ul/span[3]/li/ul/span[1]/li/a"));
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
act.moveToElement(element2).click(element2).build().perform();
Please try this and let me know if this works for you too!!
Also, consider using relative XPaths instead of the absolute XPaths you have used. The second Action is not required. One action should do the trick.
EDIT Based on user comment
Move to element1 is working and the list is getting displayed. So I am assuming that element2 is visible and so we can directly click on it without using the Actions class
Actions act=new Actions(driver);
act.moveToElement(element).build().perform();
element2.click();

Categories

Resources