I'm trying to locate an element with Selenium. But it gives an error saying:
Unable to locate an element with the xpath expression //li[normalize-space()='Don't check credit card BINs'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li[normalize-space()='Don't check credit card BINs']' is not a valid XPath expression.
I've tried searching the element by inspect and indeed the element cannot be read/found. Is there a special way on writing code for Can't word? Because I suspect that ' symbol cannot be located by selenium.
Here is the dropdown menu that I want to locate (the Don't check credit card BINs menu):
As you can see above, that the specific text is not found by xpath.
You are missing "" in your xpath, as I can see this in you HTML source, also to ignore apostrophe in Don't you can try below
"//li[normalize-space()=\"Don't check credit card BINs\"]"
If this does not work you can try below one too.
"//li[contains(text(),\"Don't check credit card BINs\")]"
Here are some things you can try.
Try using javascript executor, happened to me a few times I can't click a dynamically loaded element with selenium builtin selectors but I was able to click them with selenium js executor
Use absolute xpath, helpful where relative xpath / css selector fails. You can use chrome extensions to get absolute xpath
Hope this helps.
Related
driver.find_elements(By.XPATH, '/html/body/div[1]/div/div[6]/div[1]/div[2]/div[2]')
The above element is returned by the selenium chrome driver but the below element (div with main-content-row class) is not.
driver.find_elements(By.XPATH, '/html/body/div[1]/div/div[6]/div[1]/div[2]/div[3]')
Try providing the relative xpath,
driver.find_elements(By.className,'main-content-row')
or
driver.find_elements(By.XPATH,"//div[#class='main-content-column']/div[3]")
Note: The XPaths can be incorrect because the given information are not enough to provide the exact relative XPath. Please do the modifications you need if it is not working.
I got stuck not just for this one case. Many cases came out with nosuchelement exception, I cannot find the reason why. I use chrome console to locate the element and it shows correct result. Is that possible something wrong with the page itself, not my script?
Context click on the element you are trying to click, and see if the element is inside any frame. Try to navigate through the tags and see if this element is under any iframe.
switch to the iframe using,
driver.switchTo().frame(driver.findElement(By.xpath("iframexpath")));
The html element Im trying to locate is the "Shared" link.
I wrote a dynamic xpath to locate the element and it shows as the element was identified in the developer console.
But when i use the xpath that i wrote in the developer console to locate the element using selenium, it does not locate the element.
The method i used to check if it locates the element is shown below.
I could not figure out why this issue occurs, Is it because of a issue in the xpath that i have written or because of another issue?
Code you can try out is :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath(" your Xpath ")));
driver.findElement(By.xpath("your Xpath")).click();
The Xpath you have written would work if only one title is present on current page.
driver.find_elements doesn't have attribute to click() so use driver.findElement instead of Elements
please help me.
Actually I want to find element locator in selenium using XPath since the id is auto-generate (the ID always changed when the page refresh). but the XPath always changed too. here is the XPath locator :
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[4]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[15]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
html/body/div[7]/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
Actually I already try to use :
html/body/div[class='scrollingMenu']/div/div[1]/div/table[1]/tbody[2]/tr[1]/td/div/nobr
the div itself has unique classname scrollingMenu. but it is not working. it always give the error element not found.
You can use element's id in your XPath as follow:
Suppose id="constantPart-generatedPart12345", then XPath is
//*[contains(#id, "constantPart-")]
PS. If this not works, update your question with HTML for target element, so I can edit XPath appropriatelly
Open the page in Google Chrome and use F12 to test your xPath before implementing it - has saved me a lot of time
I have a textfield in my application , it gets enabled when i click on it. I am able to do click using CSS but i am unable to enter text when it got highlighted.
I tried using xpath to idenfify the dynamicly but webdriver is throwing an error
"Unable to locate element:
{"method":"xpath","selector":"//input[#class='x-form-field
x-form-text']"}![enter image description here][1]
showed the difference before n after clicking on the field in thescreen shot.
Please help in how to dynamically identify and enter the text in that field
try the following:
String cssSelector = "[class='x-form-field x-form-text']"
//but verify found css selector in firepath, firebug addon in ffox to make sure selenium //locate web element for input properly.
driver.findElement(By.cssSelector(cssSelector)).clear();
driver.findElement(By.cssSelector(cssSelector)).sendKeys("blablabla");
Hope this works for you
Try this code:
String cssSelector = ".x-form-field x-form-text"
driver.findElement(By.cssSelector(cssSelector)).clear();
driver.findElement(By.cssSelector(cssSelector)).sendKeys("blablabla");
Let me know is the above scripting is working or not.