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.
Related
I am doing automation using selenium webdriver (java) on a search engine BookMyCrop (http://www.bookmycrop.com). Here, I searched for a crop but, I am not able to click on desired search result. Please help me with it.
Code :
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
------My 1st try-------------
//WebElement link = driver.findElement(By.xpath("\"//div[#id = 'Links']/a[3]\""));
//link.click();
------My 2nd try-------------
//List<WebElement> find = driver.findElements(By.xpath("/html/body/section[2]/div[2]/div/div/div/div[1]"));
//find.get(1).click();
}
} –
You can use the css selector based on class names: ".product-block.inner-product-block" and get the list of all the search results.
Then click on whatever index you want to click.
I am not using an IDE for this but it would look something like this:
driver.findElements(By.cssSelector(".product-block.inner-product-block")).get(0).click();
As said, you can try with css ".product-block.inner-product-block"
Then
get List of WebElements
do loop
inside loop, try get text of each element or innerText attribute
cross check if it is required one or not by simple if condition
If so, click on that web element and break loop
if this locator is not giving required info, try other locator. say $$("a h3") for veg names.
The below code worked for me. It is navigates to the correct link
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.bookmycrop.com");
WebElement search = driver.findElement(By.xpath("//*[#id=\"search_keyword\"]"));
search.sendKeys("59825");
search.sendKeys(Keys.ENTER);
driver.findElement(By.partialLinkText("Cashew")).click();
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");
I'm facing an issue writing a Selenium program using Java.
I'm trying to click the following link :
חפש
by using the following code:
WebDriver driver = new HtmlUnitDriver();
driver.get("http://mapi.gov.il/Pages/LotAddressLocator.aspx");
((HtmlUnitDriver) driver).setJavascriptEnabled(true);
WebElement element = driver.findElement(By.id("AddressInput"));
element.sendKeys("הנגיד 16");
WebElement button = driver.findElement(By.id("helkaSubmit"));
button.click();
String pageSource=driver.getPageSource();
System.out.println(pageSource);
But it doesn't seems like the button was clicked at all, since the page content which must change after clicking the link, does not change at all.
I need to find a way to check whether the link was clicked at all, or to find why it was not clicked.
Can anyone point me to the solution ?
EDIT:
Using executor.executeScript("document.getElementById('helkaSubmit').href='http://www.google.co.il';");
executor.executeScript("document.getElementById('helkaSubmit').click();");
Worked just fine, so javascript is enabled and clicking is executed, but for some reason the original code does nothing.
try this,
driver.findElement(By.id("helkaSubmit")).click();
I have searched all the forums but I didn't get a correct answer for my issue. My web page to test has a link hidden below, and I am trying to find it manually by searching for it with xpath or the ID attribute of the element, but I am not able to find it when I am running the web driver script. Even when it is not giving any error on that element, I am getting an error on next command/line.
I found below code from the forums, which is scrolling whole page. I don't want this, I want to scroll down vertically in a specific div area as in screen shot.
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("ctl00_Menu1_scrollDiv.scrollBy(0,250)", "");
div id for this is "ctl00_Menu1_scrollDiv"
Element id: ctl00_Menu1_DlMenu_ctl09_LnkMenuname
Please help me on this issue. Thanks in advance.
Help will be appreciated.
This is umendra tomar , I have found very simple solution for this, please use the below code for scroll a specific div in html using selenium .
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('scrollCustom').scrollTop= 450");
scrollCustom = this is the ID of your div which you want to scroll.
document.getElementById = this is use in javascript to locate a webelement.
Don't worry we can use this in java using javascriptExecutor
Check if this works for you.
var s = $('#ctl00_Menu1_scrollDiv').scrollTop();
This will give the current value of the scroll in the div.Use this only if you want to scroll inside a div to a certain point dynamically. Otherwise you can hardcode the scrollTop value inside animate()
Using the current value of your scroll you can parameterize the given below scrollTop parameter
$("#ctl00_Menu1_scrollDiv").animate({ scrollTop: "100px" }); // Here 100 px is just an example
I had used this to scroll a large div programmatically in my webdriver framework. Also, this will work if your AUT has jQuery loaded in the browser.
In Java:
JavascriptExecutor js;
js = (JavascriptExecutor) driver;
js.executeScript("$(\"#ctl00_Menu1_scrollDiv\").animate({ scrollTop: \"100px\" })");
First you should not just reference an element by the id. You should set scrollTop to scroll it to a position.
document.getElementById("ctl00_Menu1_scrollDiv").scrollTop(250);
Non-JQuery solution based on this post.
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollTop=arguments[1].offsetTop",
divWithScrollbarElement,
elementToScrollTo);
where divWithScrollbarElement is the div element which you are looking to scroll, and elementToScrollTo is the child element which you want to make viewable (which in my case was actually the parent of the element which I was initially trying to view). If elementToScrollTo is not actually in the DOM yet, you may need to use the script once to scroll down as far as possible, and then again once more elements have loaded.
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