简体   繁体   English

在python selenium中用get_attribute()找到xpath

[英]Find the xpath with get_attribute() in python selenium

This is a somewhat backwards approach to web scraping.这是 web 抓取的一种有点倒退的方法。 I need to locate the xpath of a web element AFTER I have already found it with a text()= identifier我需要找到 xpath 的 web 元素后,我已经找到它与 text()= 标识符

Because the xpath values are different based on what information shows up, I need to use predictable labels inside the row for locating the span text next to found element.因为 xpath 值根据显示的信息而不同,所以我需要在行内使用可预测的标签来定位找到的元素旁边的跨度文本。 I found a simple and reliable way is locating the keyword label and then increasing td integer by one inside the xpath.我发现一种简单可靠的方法是定位关键字 label,然后在 xpath 中将 td integer 增加一个。

    def x_label(self, contains):
         mls_data_xpath = f"//span[text()='{contains}']"
         string = self.driver.find_element_by_xpath(mls_data_xpath).get_attribute("xpath")
         digits = string.split("td[")[1]
         num = int(re.findall(r'(\d+)', digits)[0]) + 1
         labeled_data = f'{string.split("td[")[0]}td[{num}]/span'
         print(labeled_data)
         labeled_text = self.driver.find_element_by_xpath(labeled_data).text
         return labeled_text

I cannot find too much information on.get_attribute() and get_property() so I am hoping there is something like.get_attribute("xpath") but I haven't been able to find it.我找不到太多关于 .get_attribute() 和 get_property() 的信息,所以我希望有类似 .get_attribute("xpath") 的信息,但我一直没能找到。

Basically, I am taking in a string like "ApprxTotalLivArea" which I can rely on and then increasing the integer after td[0] by 1 to find the span data from cell next door.基本上,我接受了一个像“ApprxTotalLivArea”这样的字符串,我可以依赖它,然后将 td[0] 之后的 integer 增加 1,以从隔壁的单元格中找到跨度数据。 I am hoping there is something like a get_attributes("xpath") to locate the xpath string from the element I locate through my text()='{contains}' search.我希望有类似 get_attributes("xpath") 的东西可以从我通过 text()='{contains}' 搜索找到的元素中找到 xpath 字符串。

我需要在行内使用可预测的标签来定位元素旁边的跨度文本

The Remote WebElement does includes the following methods: Remote WebElement确实包括以下方法:

But xpath isn't a valid property of a WebElement.但是xpath不是 WebElement 的有效属性。 So get_attribute("xpath") will always return NULL所以get_attribute("xpath")将始终返回NULL

I was able to find a python version of the execute script from this post that was based off a JavaScript answer in another forum.我能够从这篇帖子中找到执行脚本的 python 版本,该帖子基于另一个论坛中的 JavaScript 答案。 I had to make a lot of.replace() calls on the string this function creates but I was able to universally find the label string I need and increment the td/span xpath by +1 to find the column data and retrieve it regardless of differences in xpath values on different page listings.我不得不对这个 function 创建的字符串进行大量的.replace() 调用,但我能够普遍找到我需要的 label 字符串,并将 td/span xpath 增加 +1 以查找列数据并检索它,无论不同页面列表中 xpath 值的差异。

def x_label(self, contains):
    label_contains = f"//span[contains(text(), '{contains}')]"
    print(label_contains)
    labeled_element = self.driver.find_element_by_xpath(label_contains)
    print(labeled_element)
    element_label = labeled_element.text
    print(element_label)

    self.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++;
        }
    }
    """)

    generated_xpath = self.driver.execute_script("return window.getPathTo(arguments[0]);", labeled_element)
    generated_xpath = f'//*[@{generated_xpath}'.lower().replace('tbody[1]', 'tbody')

    print(f'generated_xpath = {generated_xpath}')

    expected_path = r'//*[@id="wrapperTable"]/tbody/tr/td/table/tbody/tr[26]/td[6]/span'

    generated_xpath = generated_xpath.replace('[@id("wrappertable")', '[@id="wrapperTable"]').replace('tr[1]', 'tr')
    clean_path = generated_xpath.replace('td[1]', 'td').replace('table[1]', 'table').replace('span[1]', 'span')
    print(f'clean_path = {clean_path}')
    print(f'expected_path = {expected_path}')
    digits = generated_xpath.split("]/td[")[1]
    print(digits)
    num = int(re.findall(r'(\d+)', digits)[0]) + 1
    print(f'Number = {num}')
    labeled_data = f'{clean_path.split("td[")[0]}td[{num}]/span'
    print(f'labeled_data = {labeled_data}')
    print(f'expected_path = {expected_path}')

    if labeled_data == expected_path:
        print('Congrats')
    else:
        print('Rats')

    labeled_text = self.driver.find_element_by_xpath(labeled_data).text
    print(labeled_text)
    return labeled_text

This function iteratively get's the parent until it hits the html element at the top这个 function 迭代获取父元素,直到它到达顶部的 html 元素

from selenium import webdriver
from selenium.webdriver.common.by import By


def get_xpath(elm):
    e = elm
    xpath = elm.tag_name
    while e.tag_name != "html":
        e = e.find_element(By.XPATH, "..")
        neighbours = e.find_elements(By.XPATH, "../" + e.tag_name)
        level = e.tag_name
        if len(neighbours) > 1:
            level += "[" + str(neighbours.index(e) + 1) + "]"
        xpath = level + "/" + xpath
    return "/" + xpath

driver = webdriver.Chrome()
driver.get("https://www.stackoverflow.com")
login = driver.find_element(By.XPATH, "//a[text() ='Log in']")
xpath = get_xpath(login)
print(xpath)

assert login == driver.find_element(By.XPATH, xpath)

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM