Java – Selenium can’t find element by xpath

elementjavaseleniumselenium-webdriverxpath

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!!

Best Answer

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.

Related Topic