Django Anonymous
Django Anonymous

Reputation: 3025

Selenium Python Finding actual xPath of current node within loop

browser = webdriver.Firefox()
browser.get("http://www.example.com/admin/orders")
total_images = len(browser.find_elements_by_xpath('/html/body/div[5]/div[3]/div[1]/div[3]/div[4]/form[1]//img'))
for i in range(1,total_images):
            compiledData['item_url'] = browser.find_elements_by_xpath(browser.xpath('/html/body/div[5]/div[3]/div[1]/div[3]/div[4]/form[1]//img[')+str(']'))

Now what I need is the actual path of the element. As you can see that I am looping through the range of images within a dynamic xpath code. Now if I am in a loop and there I need the exact xpath of that current dynamically identified image xpath

For example, if it is running for the first image then it should give:

/html/body/div[5]/div[3]/div[1]/div[3]/div[4]/form[1]/img[1]

if it's running for second time then:

/html/body/div[5]/div[3]/div[1]/div[3]/div[4]/form[1]/img[2]

I want to extract this actual xpath of that current node within that loop

Now something like the PHP one:

$breakup = $xpath->query('/html/body/div[5]/div[3]/div[1]/div[3]//*[text()="Package total"]');
if($breakup->length != 0)
{
    $breakupPath = substr($breakup->item(0)->getNodePath(), 0, -2)."2]";
    $orderData['total'] = str_replace("Rs.", "", trim($xpath->query($breakupPath)->item(0)->nodeValue));
}

Where, $breakup->item(0)->getNodePath() gives the real xPath of the dynamically identified xPath

Upvotes: 3

Views: 1424

Answers (1)

alecxe
alecxe

Reputation: 474161

One option here would be to use one of the XPath generating functions suggested here:

And execute them using execute_script(). Example code:

from selenium import webdriver


driver = webdriver.Firefox()
driver.get("http://www.w3schools.com/")


driver.execute_script("""
window.getPathTo = function (element) {
    if (element.id!=='')
        return 'id("'+element.id+'")';
    if (element===document.body)
        return element.tagName;

    var ix= 0;
    var siblings= element.parentNode.childNodes;
    for (var i= 0; i<siblings.length; i++) {
        var sibling= siblings[i];
        if (sibling===element)
            return window.getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
        if (sibling.nodeType===1 && sibling.tagName===element.tagName)
            ix++;
    }
}
""")

element = driver.find_element_by_class_name("w3-fluid")

generated_xpath = driver.execute_script("return window.getPathTo(arguments[0]);", element)
print(generated_xpath)

Another option would be to get the source of a page and feed it to lxml.html HTML parser which has getPath() method:

from selenium import webdriver
import lxml.html


driver = webdriver.Firefox()
driver.get("http://www.w3schools.com/")

tree = lxml.html.fromstring(driver.page_source)
root = tree.getroottree()

driver.close()

element = tree.xpath("//*[@class='w3-fluid']")[0]
print(root.getpath(element))

Upvotes: 4

Related Questions