Tried with CSS selector and xpath but nothing is working for the button that i need to click
up vote
0
down vote
favorite
I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."
HTML CODE:
<div class="details__selectItem">
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>
I tried with following locators:
driver.findElement(By.linkText("Select Lens Type")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();
driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();
driver.findElement(By.className("details__selectItem")).click();

selenium webdriver
add a comment |
up vote
0
down vote
favorite
I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."
HTML CODE:
<div class="details__selectItem">
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>
I tried with following locators:
driver.findElement(By.linkText("Select Lens Type")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();
driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();
driver.findElement(By.className("details__selectItem")).click();

selenium webdriver
1
Post your code.
– Guy
Nov 22 at 5:43
1
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."
HTML CODE:
<div class="details__selectItem">
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>
I tried with following locators:
driver.findElement(By.linkText("Select Lens Type")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();
driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();
driver.findElement(By.className("details__selectItem")).click();

selenium webdriver
I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."
HTML CODE:
<div class="details__selectItem">
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>
I tried with following locators:
driver.findElement(By.linkText("Select Lens Type")).click();
driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();
driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();
driver.findElement(By.className("details__selectItem")).click();

selenium webdriver
selenium webdriver
edited Nov 22 at 16:37
Skynet
4,24652640
4,24652640
asked Nov 22 at 3:10
Rozmeen
2816
2816
1
Post your code.
– Guy
Nov 22 at 5:43
1
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39
add a comment |
1
Post your code.
– Guy
Nov 22 at 5:43
1
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39
1
1
Post your code.
– Guy
Nov 22 at 5:43
Post your code.
– Guy
Nov 22 at 5:43
1
1
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:
int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here
WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();
Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));
If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:
int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here
WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();
Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));
If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.
add a comment |
up vote
0
down vote
The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:
int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here
WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();
Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));
If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.
add a comment |
up vote
0
down vote
up vote
0
down vote
The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:
int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here
WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();
Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));
If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.
The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:
int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here
WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();
Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));
If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.
answered Nov 22 at 15:47
KjetilNordin
1,40042968
1,40042968
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423328%2ftried-with-css-selector-and-xpath-but-nothing-is-working-for-the-button-that-i-n%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Post your code.
– Guy
Nov 22 at 5:43
1
@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48
These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36
@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39