I tried this xpath to retrieve a button
By addGhostButton = By.xpath("//button[#data-test-id=\"order\"]");
Selenium did not find it, I had this error invalid selector: Unable to locate an element with the xpath expression
Maybe the syntax is wrong ? any help please?
You haven't provided the complete error stack trace and the relevant HTML. However, the xpath you constructed is pretty much valid and legit. Hence you shouldn't see invalid selector error but Unable to locate an element with the xpath expression is pretty much possible.
The desired element looks to be a dynamic element so to locate the element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-test-id='order']")));
Using xpath:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#data-test-id='order']")));
Try with this xpath //button[Contains(text(),"Précommande maintenant")]`
Related
So I have the following HTML Tag I want to fetch with Selenium:
<button data-v-48e2f75a="" class="app-button rose-button min-width">Sign in</button>
I am trying to fetch it like so:
...
browser.findElement(By.cssSelector("app-button rose-button min-width")).click();
When I run this it always returns the following exception:
org.openqa.selenium.NoSuchElementException: Unable to locate element: app-button rose-button min-width
This is one of the methods I've tried already however it has not worked either:
browser.findElement(By.cssSelector("app-button rose-button min-width")).click();
The css selector is wrong. Try it in this way:
browser.findElement(By.cssSelector(".app-button.rose-button.min-width")).click();
It should be By.className instead of cssSelector.
Example:
WebElement parentElement = driver.findElement(By.className("rose-button"));
If you want to use cssSelector, it should look like:
driver.findElement(By.cssSelector("button[class='app-button rose-button min-width']"));
Another way is using xpath:
driver.findElement(By.xpath("//div[#class='app-button rose-button min-width']"));
And as DebanjanB mentioned, you can add webdriverwait also, if there is some kind of loading delay.
To click on the element with text as Sign in you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.app-button.rose-button.min-width"))).click();
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='app-button rose-button min-width' and text()='Sign in']"))).click();
Reference
You can find a couple of detailed discussions in:
NoSuchElementException, Selenium unable to locate element
Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id='login-email']
I am new to selenium and trying to write linktext code for the html code
Programmer
Code trial:
driver.findElement(By.linkText("Programmer")).click();
the code above is not working, is something wrong?
linkText should work if the element with the text Programmer is uniquely identified within the HTML. Still as the element is a JavaScript enabled element so to click() on the element you need to use elementToBeClickable() and you can use the following Locator Strategy:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Programmer"))).click();
Alternative
As an alternative you can also use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.labels[href^='repopulateUser']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='labels' and starts-with(#href, 'repopulateUser')][text()='Programmer']"))).click();
I have searched for an Account in Salesforce and it gives a few search results. But I'm unable to find the element in Selenium.
I have tried using absolute/relative xpath and CSSSelector, LinkText as well.
Used: driver.findElement(By.linkText("ILT_Order1")).click();
ILT_Order1
I am unable to find the element.
Induce WebDriverWait and elementToBeClickable and following xpath.
If still not found then check if there any iframe available on the page.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-seclke='Account'][text()='ILT_Order1']")));
element.click()
As the element is a JavaScript enabled element so to click() on the element you need to use elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("ILT_Order1"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[data-seclke='Account'][onmousedown^='searchResultClick'][href]"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-seclke='Account' and text()='ILT_Order1'][starts-with(#onmousedown, 'searchResultClick')]"))).click();
My element is displayed as:
<a class="main-item" href="#">Business Loans</a>
xpath is:
//*[#id='main-nav']/ul/li[1]/a[1]']
This returns invalid element locator
//*[#id='main-nav']/ul/li[1]/a']
driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[1]/a[1]']"))
I am trying to get the element.
Welcome to SO. Here is the simple xpath.
//*[#id='main-nav]//a[#class='main-item' and .='Business Loans']
If you want to use the one which you mentioned, here is the corrected.
driver.findElement(By.xpath("//*[#id='main-nav']/ul/li[1]/a[1]"))
Try contains function in xpath, it can extract all the elements which matches a particular text value
//a[contains(text(),'Business Loans')]
This error message...
org.openqa.selenium.InvalidSelectorException: invalid selector
...implies that your xpath was not a valid one.
You can't use the single quote i.e. ' or the double quote i.e. " for both By value and attribute values.
Solution
You can use either of the following Locator Strategies:
cssSelector:
WebElement element = driver.findElement(By.cssSelector("#main-nav a.main-item"));
xpath:
WebElement element = driver.findElement(By.xpath("//a[#class='main-item' and text()='Business Loans']"));
In the site [https://javapapers.com/core-java/java-qr-code/] if I search for element with below xpath in browser
//div[#id='tutorial-body']/p/img
I am getting two matches, but if I use the same thing in selenium code
WebDriver driver = new FirefoxDriver();
driver.get("https://javapapers.com/core-java/java-qr-code/");
Thread.sleep(2000);
WebElement qr_url = driver.findElement(By.xpath("//div[#id='tutorial-body']/p/img/"))
String l =qr_url.getCssValue("src");
I am getting below error:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Given xpath expression "//div[#id='tutorial-body']/p/img/" is invalid: SyntaxError: The expression is not a legal expression.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
So what's wrong in my Xpath?
Just remove extra slash in the end of expression:
"//div[#id='tutorial-body']/p/img/" --> "//div[#id='tutorial-body']/p/img"
Also to aid you with xPath and avoid errors like this, you can Inspect and copy xPath using Chrome and enter the same value in selenium selector.