使用java的空间webdriver类名 [英] webdriver classname with space using java

查看:117
本文介绍了使用java的空间webdriver类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题在jquery中得到了很好的答案,我想知道是否有人能用Java提供这个的例子吗?

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(当前时间))。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();

编辑#1 根据提供的html,

Edit#1 Based on html provided,

查看评论中的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(此处为部分链接文字) ;

您还可以将xpath用作:

You can also use xpath as:

WebElement element = driver.findElement(By.xpath("//a[@class='current time']"));
element.click();

OR,

WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();

这篇关于使用java的空间webdriver类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆