Reputation: 53
When I execute the following code:
driver.findElement(By.className("qview-product-name")).click();
I get the following error
Session ID: d5df6f837164b1738991e8dc09027fe0
*** Element info: {Using=class name, value=qview-product-name}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByClassName(RemoteWebDriver.java:412)
at org.openqa.selenium.By$ByClassName.findElement(By.java:389)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at Logins.bcLogin(Logins.java:140)
at Exception.main(Exception.java:54)
The webpage I am working on definately contains the following HTML code and I have tried waiting an appropriate amount of time to execute.
<dd class="qview-product-name">
<span class="note">1 x </span>
<a href="Link_here"_blank">Title</a>
</dd>
I thought I was getting pretty good at locating elements using the various methods, but this has me stumped. Any ideas on how I should go about troubleshooting? Thanks!
Upvotes: 3
Views: 15660
Reputation: 193078
There are a couple of things you need to take care of:
By.className("qview-product-name")
refers to the parent <dd>
tag and perhaps is not the desired element you want to click. Rather your usecase must be to click on the <a href="Link_here"_blank">Title</a>
element.As per best practices, while invoking click()
you need to induce you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
linkText
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Title"))).click();
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("dd.qview-product-name a[href='Link_here']"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//dd[@class='qview-product-name']//a[@href='Link_here' and text()='Title']"))).click();
Ensure that:
@Test
as non-root user.Upvotes: 2