硒无法通过xpath找到元素

硒无法通过xpath找到元素

本文介绍了硒无法通过xpath找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了一段时间.我正在使用Selenium和WebDriver 2.33版(与所有浏览器一起使用).我正在使用Java,这应该是任意的.我正在做的只是简单地找到一个元素并将其悬停在上面,这是我在较早的代码中所做的.但是由于某种原因,我无法使它正常工作.我正在尝试使用此xpath来获取元素,方法是右键单击Chrome中HTML中的元素,然后单击复制xpath":

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]

这就是我试图获取元素的方式(由于"highcharts-10"动态变化):

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输入正确(我想尝试一堆酒吧)

barOption is inputting correctly (there are a bunch of bars that I'm trying to go through)

这是我的Java代码:

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

我在这里做错了什么?我尝试使用switchTo()语句,但是没有可以正确切换到的iframe.这是HTML图片,因为我无法掌握实际文本:

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:

更新的HTML链接: http://i1250.photobucket.com/albums/hh527/dr4g1116/Capture_zps6e2bcc1b9.png

有人对我有什么建议吗?请让我知道我做错了!

Anyone have any suggestions for me? Please let me know what I'm doing wrong!

谢谢!

推荐答案

CSS Selectors :

By by = By.css('div[id^="highcharts"] g[class^="highcharts"] > g > rec')

我使用了

g.class_name,因为<g>标记类名不可见.将该类名替换为正确的类名.

g.class_name I used,as that <g> tags class name is not visible. Replace that class name with the proper class name.

这篇关于硒无法通过xpath找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:59