I am currently trying to map the highlighted line shown below in the screen grab.
I've looked at w3schools and looked it up here on SO but cant seem to get my code right.. My selenium script keeps erroring out with cannot identify element.
currently, I am doing something like:
selenium.click("xpath=//table[#id='resultTable']/tbody/tr[#class='level3']/td[#id='resultTable_0_0_1_ob']/span/a[#class='linkOnly']");
I have also tried this:
selenium.click("xpath=//table[#id='resultTable']/tbody[1]/tr[6]/td[2]/span[1]/a[1]");
and this:
selenium.click("xpath=//table[#id='resultTable']/tbody/tr[6]/td[2]/span/a");
Am I doing it right and I just need to put in a delay ? or am I doing this completely wrong ?
EDIT:
Here is the code snippet as requested. Thanks for the pointer, I didn't really notice that there were multiple classes with the same name! Hm, but for some reason, the other two XPaths that I wrote dont work.
In this code snippet, I expanded the table so you can better see how the table is set up. Again, sorry for the image size, but ctrl+scroll up should enlarge the picture.
I'd prefer using css selectors as they work faster (currently using webdriver + java)
so solution to your problem be like:
String cssSelector = "tr[class='level3']>td[id='resultTable_0_0_1_ob']>span>a[class='linkOnly']"
driver.findElement(By.cssSelector(cssSelector)).click():
driver.manage.timeouts.implicitWait(3,TimeUnit.SECONDS);
As you got ID='resultTable_0_0_1_ob' I think this ID should help in finding unique element on the page.
2nd way to solve your problem:
also if framework which your site is implemented on supports jQuery you can easly use jQuery:
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $("tr[class='level3']>td[id='resultTable_0_0_1_ob']>span>a[class='linkOnly'];");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
And don't forget to verify your xpaths, css selectors in firepath(firebug extension) in firefox.
Picture below provided:
Hope this helps you.
xpath=//table[#id='resultTable']/tbody/tr[#class='level3']/td[#id='resultTable_0_0_1_ob']/span/a[#class='linkOnly']
The Corresponding CSS is css=#resultTable > tbody > tr.level3 > #resultTable_0_0_1_ob > span > .linkOnly
Related
I'm working on a program that uses Selenium and I'm trying to scroll on some aside div of the document.
I firstly tried the following line of code but this only scrolls the main part of the HTML document:
((JavascriptExecutor) driver).executeScript("scrollBy(0, 500)");
So I tried to execute this:
((JavascriptExecutor) driver)
.executeScript("document.getElementsByClassName('aside-div')[0].scroll(0,100)");
This compile and executes without any error but the section I want to scroll through doesn't scrolls. How can I do this?
The best practice is to always use the javascript code on your browser console before actual implementation.
Also, plz make sure scroll(x,y), the value should be according to the scroll it can be either (1000,0) or (0,1000) negative-positive according to the requirement. Kindly choose accordingly.
Ok so coming back to your actual problem, what I feel is you are incorrectly using the code
Either you have been using scroll(0,1000) instead of scroll(1000,0) so on..
You have not mentioned the exact element locator.
PS: Please provide the HTML for in-depth debugging.
Meanwhile, you can checkout the below code:
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(0,1000)");
//OR
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(1000,0)");
//OR
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div"));
((JavascriptExecutor) driver).executeScript("arguments[0].scroll(1000,0);", elementToScroll);
Please provide the exact element locator to use below code:
Example:
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div table tr td"));
//This will scroll the page Horizontally till the element is found
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", elementToScroll);
I have html code:
<div class="formCaptionContainer fill-width" data-dyn-bind="sizing: { width: $dyn.layout.Size.available }">
<h1 class="formCaption" data-dyn-bind="text: $data.Caption, click: $data.GoGridView">Expense report for Aditi Mehta - GS1-000282, testing2</h1>
<h2 class="formCaption-context" data-dyn-bind="text: $data.ParentTitleFields">Aditi Mehta : GS1-000282</h2>
</div>
I want to get the value Expense report for Aditi Mehta - GS1-000282, testing2 from /h1 tag
Any one know how to do it?
I've tried :
By.xpath(".//div[#class='formCaption']/h1";
Above showing no element found
By.className("formCaption");
Above showing blank data
By.xpath(".//*[#class='formCaption']/h1");
Above showing no element found
This code work for me very well
String textprint=driver.findElement(By.xpath("//h2[#class='formCaption-context']")).getText();
System.out.println(textprint);
Hope It will solve your problem
Try this one
String msg= driver.findElement(By.xpath(//div[contains(#class='formCaptionContainer')]/h1 ).getText();
Can you try the below Xpath.
String msg=driver.findElement(By.xpath("//div[#class='formCaptionContainer fill-width']/h1[#class='formCaption-context']")).getText();
As per the HTML you have shared, the node attributes looks dynamic to me. So we have to induce WebDriverWait as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h1[#class='formCaption']"), "Aditi Mehta - GS1-"));
System.out.println(driver.findElement(By.xpath("//h1[#class='formCaption']")).getAttribute("innerHTML"));
You need to consider your locator strategy, the following provide good preferences for general case approach.
location by ID globally unique elements
locate by name for page unique elements
locate by css as catch all case
You should also take a look at the PageObject pattern.
It appears the page is using a JS library to display the contents through late binding/resolution. Your test needs allow for this and wait until the page elements you are interested in are fully rendered. Find out from the developers what the element looks like before it is resolved and afterwards. Late resolution is one considerations in determining your locator strategy. The strategy should include fluent waiting and default time-outs for elements to appear.
This is not a problem of fixing a single locator.
Thanks all for your help. It is working fine now. I use the below code
By headingExpenseText = By.xpath("//*[#class='formCaption'][contains(text(),'Expense report for')]");
public String getTitleEx()
{
return driver.findElement(headingExpenseText).getAttribute("innerHTML");
}
This worked for me:
String titleElem = driver.findElement(By.xpath("//*[#class='formCaptionContainer fill-width']/h1")).getAttribute("innerHTML");
assertEquals("Worked", titleElem);
What do you need exactly, Data displayed on web page or textValue used in HTML code?
If your locator is correct , then instead of getText(), you can try getAttibute("innerHTML") like below,
By.xpath("//*[#class='formCaption'][contains(text(),'Aditi Mehta')]").getAttribute("innerHTML");
I have a web system I am automating using Java/Selenium Webdriver. I have an item I am trying to get access to. It has a compound class name. I have tried all the solutions I have been able to find here and so far none of them work.
The most offered solution looks like this:
By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());
When I check the size of the elements array it is always zero.
What I am finding however is that when I put the selector string in to the Selenium IDE (2.9.1) and use the "Find" button it identifies the correct web element without any problem at all.
I am at a loss for why it works in the IDE but not in my code.
Try selecting the element using its XPath? In the past when I ran into issues trying to select something using cssSelector, I often had success when I tried its XPath instead.
Give some wait time before the selector you are taking.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());
or try to find the elements with the help of Xpath or id.
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
Hope it will help you
I'm trying to click the link below:
<td id="mainleftlinkzoneover" class="mainleftlinks" width="151" title="Student Medicaid Eligibility">
Here's the code. It works with other links like this but not this one.
WebElement myElement = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("td[title='Student Medicaid Eligibility']")));
myElement.click();
Anyone have any ideas what I'm doing wrong?
you can do it by using the below code:
By.id("mainleftlinkzoneover") //here only id is used
or
By.cssSelector("#mainleftlinkzoneover[title='Student Medicaid Eligibility']") // this is another way to locate element
use the any one instead of your By.cssSelector.
Make sure there are no frame.
Is the link available for others to check? I mean can I test it if I want? you can give the url if possible.
here are my 2 cents:
I am thinking because of the spaces in your title, you might not be able to click on it.
I would try using contains
something like By.xpath("//td[contains(#title,'Eligibility')]") you can put either student or medicaid if they are unique. See if that helps.
I've been working at this for some time now. I'm using Selenium and WebDriver version 2.33 (with all browsers). I'm using Java, which should be arbitrary. What I'm doing is simply find an element and hover over it, which I have done in earlier code. But for some reason, I can't get this one to work. I'm trying to get an element with this xpath, obtained by right-clicking the element in the HTML in Chrome and clicking "copy xpath":
//*[#id="highcharts-10"]/svg/g[7]/g/rect[1]
This is how I'm trying to get the element (due to "highcharts-10" dynamically changing):
//*[starts-with(#id, 'highcharts')]/svg/g[7]/g/rect[" + barOption + "]
barOption is inputting correctly (there are a bunch of bars that I'm trying to go through)
Here is my Java code:
WebDriverWait wait = new WebDriverWait(getWebDriver(), 5);
WebElement element;
WebDriver driver = getWebDriver();
By by = By.xpath("//*[starts-with(#id, 'highcharts')]/svg/g[7]/g/rect[" + barOption + "]");
Actions action = new Actions(driver);
WebElement elem = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
action.moveToElement(elem);
action.perform();
What am I doing incorrect here? I've tried using switchTo() statements, but there are no iframes that I can correctly switch to. Here is a picture the HTML because I can't get my hands on the actual text:
UPDATED HTML LINK:
http://i1250.photobucket.com/albums/hh527/dr4g1116/Capture_zps6e2bc1b9.png
Anyone have any suggestions for me? Please let me know what I'm doing wrong!
Thanks!!
Try as CSS Selectors :
By by = By.css('div[id^="highcharts"] g[class^="highcharts"] > g > rec')
g.class_name I used,as that <g> tags class name is not visible. Replace that class name with the proper class name.
I just wanted to give a slight update on this. It seems selenium can't see past the SVG tags, so with that being said, I need to find a method to see around them...I will report back if I'm able to find out how.
Thanks all!
From your discussion with Amey i deduced that you have only one highchart. so try directly searching for element "highcharts-tracker" using classname i.e By.ClassName("highcharts-tracker") and then hover on this element itself. This would exactly do what you want to achieve.
Sorry just seeing your comment on my earlier answer.
You can get the values for each bar in barchart by following way:
var barValues = new List<string>();
var actions = new Actions(webDriver); //webDriver is instance of selenium WebDriver.
var chartSeriesGroup = webDriver.FindElement(By.ClassName("highcharts-series-group"));
var chartSeries = chartSeriesGroup.FindElement(By.ClassName("highcharts-series"));
var rectTags = chartSeries.FindElements(By.TagName("rect")); //To get all bars in barchart.
foreach (var rect in rectTags)
{
actions.MoveToElement(rect).Perform(); //Hover mouse on bar.
var trendMarkers = webDriver.FindElement(By.ClassName("highcharts-tooltip"));
barValues.Add(trendMarkers.Text); //Storing tooltip value of bar for later use.
}
I am using same method in my current project for getting values of bars in bar chart. Hope this will help you.
Note : If tooltip for bar shows other information e.g.name etc along with value then you need to write logic for extracting the value part from the complete information stored in barValues.