Unable to locate 3rd div element - selenium

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.

Related

Selenium unable to locate an element

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.

How to handle changing of absolute xpath?

Html:
<div class="component-multi-row-slide-title"> Cloth </div>
The xpath
/html/body/div/main/div/div/div[2]/div/div/div/div[1]/div/div***[4]***/div/div[2] the div[4]
keep changing. If new item come into web, developer will change the div number.
I want to click on 'Cloth'. The xpath keep changing when new category is added.
To start with using absolute xpath have certain disadvantages, as when new elements are added the xpath of the previously identified elements will keep on changing. Hence the solution would be to use Relative Xpath.
As per the HTML you have provided, to click on the element with text as Cloth you can use the following Locator Strategy:
XPath A:
//div[#class='component-multi-row-slide-title' and contains(., 'Cloth')]
XPath B:
//div[#class='component-multi-row-slide-title' and normalize-space()='Cloth']
Absolute Xpath: It contains the complete path from the Root Element to the desire element.
Relative Xpath: This is more like starting simply by referencing the element you want and go from the particular location.
You use always the Relative Path for testing of an element. The reason behind that is if you make any architectural change in the website the change won't effect the testing or selecting of the element.
So Use relative xpath which is as follows
WebElement cloth = driver.findElement(By.xpath("//div[normalize-space()='Cloth']"));
cloth.click();
Let me know if this doesn't help
I would recommend to use customized xpath. As given below:
driver.findElement(By.xpath("//div[contains(text(), 'Cloth')]").click
driver.findElement(By.xpath("//div[contains(text(), 'Fashion Acessories')]").click
It will work for sure. Feel free to let me know if something is wrong regarding xpath.
Thanks
You need to use relative xpath in this case.
You can use any xpath from the below options. The first two will find the element by text and the 3rd option by class.
1>>driver.findElement(By.xpath("//div[contains(text(),'Cloth')]")).click();
2>>driver.findElement(By.xpath("//*[contains(text(),'Cloth')]")).click();
3>>driver.findElement(By.xpath("//div[#class='component-multi-row-slide-title']")).click();
Let me know if the same class name exist for other elements also.
Just for you reference: XPath is used to find the location of any element on a webpage using HTML DOM structure.
The disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.
So, in that case Relative Xpaths can be used:
//div[contains(text(),'Cloth')] or //*[contains(text(),'Cloth')]
//div[contains(text(),'Fashion Acessories')] or //*[contains(text(),'Fashion
Acessories')]
How to use:
driver.findElement(By.xpath("//div[contains(text(),'Cloth')]"));
You write the following code
driver.findElement(By.xpath("//div[normalize-space()='Cloth']").click
The above code will identify the element directly and also it would not be brittle in nature when developer changes the page elements.

How can i locate an element in my DOM when its class name is dynamic?

I am trying to locate and element on my web page, but the element i want to locate has no unique Id's or anything of that matter. I have only a dynamically changing class name.
I tried using Absolute Xpath but its too brittle.
<div class="frca889813467244cd88b375adbb9452c2s4" style="left:274.05px;top:141.75px;width:94.5px;height:18.9px;"><div
class="frca889813467244cd88b375adbb9452c2s11">6000.00</div></div>
From above DOM i want to get the value every time i locate the above element (Here its 6000.00 but might change in other tests).
I will suggest going with absolute XPath, I know you mentioned you have a hard time creating absolute XPath. I guess below note can help
Note:- if you copy XPath from Firefox it will mostly give you absolute XPath whereas chrome on another side will give relative
or the other way is to make an XPath using another stable element in the tree
I hope this workout for yours. if you can share a link or inspect element snapshot showing Full DOM I can help you even better. Thanks :-)

WebElement is identified through xpath in the developer console but the element wont be identified through selenium findElement method

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

Finding XPath on Selenium

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