使用java的带有空格的webdriver类名
这个问题在 jquery 中得到了很好的答案,我想知道是否有人可以在 Java 中给出 this 的示例?
This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?
我在做 driver.findElement(By.className("current time")).click();
空间是问题,我在链接上看到了解释,但是我'不知道如何在java中处理它,并且无权更改类名.
I'm doing driver.findElement(By.className("current time")).click();
The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.
我在 firefox 检查 id 中得到的粘贴示例:下面带有 cssSelector 的示例不起作用,但我可能遗漏了一些东西.
Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.
<span>
<a class="current time" href="http://someurl/" onclick="s_objectID="http://someur/">url</a>
</span>
推荐答案
您可以使用 css 选择器代替类名.您没有提及当前时间"类的标记名.我假设它是输入的,所以你的 css 选择器可以工作,
Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,
WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();
Edit#1基于提供的html,
查看您评论中的 html,您似乎有很多选项可以找到 webElement.这是您的选择,
Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,
WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();
或者这也应该有效,
WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();
你也可以使用linkText,因为元素是链接.从您提供的 html 中,链接文本是 'url'
You can also use linkText since the element is link. From the html you provided, the link text is 'url'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
也可以使用By.partialLinkText("partial link text here");
您也可以将 xpath 用作:
You can also use xpath as:
WebElement element = driver.findElement(By.xpath("//a[@class='current time']"));
element.click();
或,
WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
相关文章