webDriver classname处理空格的方法



http://stackoverflow.com/questions/7475449/webdriver-classname-with-space-using-java

This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?

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.

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>

Answers


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 Based on html provided,

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

or this should work too,

WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();

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

You can also use By.partialLinkText("partial link text here");

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值