Unable to click on images selector SELENIUM JAVA - java

I am unable to click on png image and encounter error.
HTML:
<a onmouseover="i2uiSetMenuCoords(this,event)" href="javascript:showMenu('9721')"><img hspace="1" src="./skins/e2-modern/images/dropdown.png" border="0px"></a>
Code:
if (navigateToDetails) {
SearchListSelectorExt selector = new SearchListSelectorExt();
//switchToFrame(getFrames(FRAME_TYPE.rcp_content));
//switchToFrame(getHeaderFrames());
WebElement element= selector.get(By.xpath("//a[contains(#src,'./skins/e2-modern/images/dropdown.png'"));
Object value = selector.getElementValue(element);
systemDocID = value.toString();
selector.clickName(systemDocID);
//selector.clickName(CustomerItem);
}

Your xpath is wrong...Use the below xpath
//a/img[contains(#src,'/skins/e2-modern/images/dropdown.png')]
Hope this helps you...kindly get back if it is not working

Try below xpath:-
//img[contains(#src,'dropdown.png')]
Here, we are directly looking for img tag such that its src attribute contains dropdown.png text.
If there are more than 1 web elements satisfying above xpath, then try to make it unique by adding extra attributes or parent.
//a/img[contains(#src,'dropdown.png')]
//img[#hspace='1' and contains(#src,'dropdown.png')]

Related

Obtain Selenium WebElement from cssSelector over HTML Input

I am quite new on Selenium (started today) and I would like to get the WebElement corresponding to the following html Input:
<input size="25" style="text-align:center;" value="http" onclick="this.select();" type="text"></input>
And then obtain its value. This is what I have tried so far:
WebElement element = driver.findElement(By.cssSelector(".text-align:center"));
String text = element.getText();
Or this:
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
But Java returns in both cases an exception:
org.openqa.selenium.InvalidSelectorException: The given selector
.text-align:center is either invalid or does not result in a
WebElement
Thank you,
Héctor
Do you have to search for the element by cssSelector?
You could give this a try:
WebElement element = driver.findElement(By.cssSelector("input[type='text']"));
If cssSelector is not necessary you could try grabbing the element by xpath.
If you use firefox, there is a plugin called FireBug which allows you to right click after inspecting the element and copying the xpath directly then using :
WebElement element = driver.findElement(By.xpath("XPATH HERE"));
EDIT: Part of post disappeared, redded it.
Your first try is slightly off
driver.findElement(By.cssSelector(".text-align:center"));
The (.) in a CSS selector indicates a CSS class name but that's a style on the element and not a class. There is no class on that element to use in that way.
Your second try looks good but maybe it's not unique on the page? Hard to tell with only the one line of HTML. You'd have to provide more of the HTML of the page. Try it again but get the value instead of text.
WebElement element = driver.findElement(By.cssSelector("input[style='text-align:center']"));
System.out.println(element.getAttribute("value"));
Does that work? You likely will have to provide some unique HTML that surrounds the INPUT that we can use to make the CSS selector more specific.

Selenium: how to get the value of hidden element which has all div tags

I would like to get the value of all div tags specified in attached. I have tried with all possible locators like classname etc, which is showing null. and tried with JavaScript also which is returning null.
Please see the screen shot and I need the selected text which is in blue color starts with "Enables enterprise IT to deploy networking services"
You need to research creating selectors as this isn't a difficult one. There are numerous approaches for this element, but here's one for you: $$("#offers-popover .description"). Obviously this is a CSS selector based on the $$ and you use getText from the Selenium API in order to scrape the element text, which is what I assume you are intending to do.
driver.findElement(By.css("#offers-popover .description")).getText();
Since your element is not visible you can try this:
String divText = driver.findElement(By.className("description")).getAttribute("textContent");
Or, if this is not the only element on the page with the class description:
WebElement popElement = driver.findElement(By.id("offers-popover"));
String divText = popElement.findElement(By.className("description")).getAttribute("textContent");

Get page element by title attribute - Selenium and Java

I'm trying to get the image which has the word "Collector" in its title and click on it.
This is the html code for the image and its link:
<a href="javascript:*command*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
<img src="*unusable link because it's only valid for the specific page*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
The <a> and <img> tags are nested in a table cell and some divs. I didn't write the html code so don't yell at me if it's ugly :p
Here is the java code where I try to do it:
WebElement trafficLight = driver.findElement(By.xpath("//img[contains(#title,'Collector')]"));
trafficLight.click();
The error I get is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//img[contains(#title,'Collector')]"}
I'm pretty sure the xpath is ok so I don't think that's the issue.
As the img WebElement is within a frame, you will need to switch focus to that frame before you perform any action on that WebElement. You can do that using WebDriver's switchTo() method like so:
driver.switchTo().frame(frameLocator);
The frame locator can be either its (zero-based) index, name or id attribute, or a previously located WebElement.
Once you have switched focus to the required frame, you should then be able to interact with the WebElement using the same code in your initial post.
Please try this. It will resolve your problem.
WebElement frameSwitch = driver.findElement(By.xpath("Give iframe Xpath location"));
driver.switchTo().frame(frameSwitch); //Switch control to iframe.
//Perform your steps (I.e Click on Image)
driver.findElement(By.xpath("//img[contains(#title,'Collector')]")).click();
driver.switchTo().defaultContent(); //Come out of iframe.

Selenium Webdriver: How to verify a text that is within h2 and duplicated class

I want to locate the Text within the below HTML code but there are two duplicated classes.
<div id="header" class="cf">
<div class="cf">
<h1>
Text
</h1>
I located but not sure if that is the best way to do it because the text might `appear some where else.
WebElement LL = driver.findElement(By.linkText("Text"));
Anyone have a better way to locate this please? THANK you in advance!
Go by the following css to identify the element more precisely. And, the id header should be unique and that should be enough to uniquely identify this element
By css = By.cssSelector("#header div.cf>h1>a");
WebElement element = driver.findElement(css );
String text = element.getText();
You can use xpath as your selector and then use WebElements getText() method to extract the text.
WebElement element = driver.findElement(By.xpath(".//div[#class='cf']/h1/a"));
String text = element.getText();

Selenium doesn't recognise xpath

I am using Selenium WebDriver to get from a drop down list values. Unfortunately I can't get it, because my code can't recognise the xpath.
Here's my code:
WebElement selector = driver.findElement(By.xpath("id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select"));
Select s = new Select(selector);
List<WebElement> options = s.getOptions();
for (WebElement wb : options) {
System.out.println(wb.getText());
}
The problem is with the 1st line (WebElement selector). In output I get something like this:
Exception in thread "main"
org.openqa.selenium.InvalidSelectorException: The xpath expression
'id('search')/x:fieldset/x:table[1]/x:tbody/x:tr[2]/x:td[1]/x:select'
cannot be evaluated
I've even tried to find by name or class, but selenium still doesn't find this list.
How to solve the problem? Thanks in advance :)
Ganjira, This is no way to write a xpath. If you find it tough to recognize item . Use Selenium IDE 'Select' button.
If you can provide sample of html page on which you try to find your element, it would be very helpful. Anyway, try to search using css selectors e.g.
WebElement selector = driver.findElement(By.css("#search > select"));

Categories