Thursday, 12 July 2018

Selenium--to get the element when class name has space

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>Based on html provided,
Looking at the HTML  it seems I have quite a few options to find the web element. 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();
I can also use linkText since the element link. From the HTML I provided, the link text is 'URL'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
I can also use By.partialLinkText("partial link text here");
I 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();

No comments:

Post a Comment

What to do to make sure HTML Table is loaded completely if implicit and explicit wait are not working.

These days I was working on a project, to implement the delete function of a listing. Such as below: Actually,  every time deleted the it...