分享

webDriver classname处理空格的方法

 小猪窝969 2015-09-29

http:///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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多