python3.8+selenium find_element_by_*定位方式语法改变

2021/10/20 12:39:26

本文主要是介绍python3.8+selenium find_element_by_*定位方式语法改变,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

上周重装系统升级了python版本从3.6升级到了3.8,发现旧的脚本不能用了,显示如下

 提示:

find_element_by_* commands are deprecated. Please use find_element() instead

看了下源码

    def find_element_by_id(self, id_) -> WebElement:
        """Finds an element by id.

        :Args:
         - id\\_ - The id of the element to be found.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn't found

        :Usage:
            ::

                element = driver.find_element_by_id('foo')
        """
        warnings.warn(
            "find_element_by_* commands are deprecated. Please use find_element() instead",
            DeprecationWarning,
            stacklevel=2,
        )
        return self.find_element(by=By.ID, value=id_)

    def find_element(self, by=By.ID, value=None) -> WebElement:
        """
        Find an element given a By strategy and locator.

        :Usage:
            ::

                element = driver.find_element(By.ID, 'foo')

        :rtype: WebElement
        """
        if isinstance(by, RelativeBy):
            return self.find_elements(by=by, value=value)[0]

        if by == By.ID:
            by = By.CSS_SELECTOR
            value = '[id="%s"]' % value
        elif by == By.TAG_NAME:
            by = By.CSS_SELECTOR
        elif by == By.CLASS_NAME:
            by = By.CSS_SELECTOR
            value = ".%s" % value
        elif by == By.NAME:
            by = By.CSS_SELECTOR
            value = '[name="%s"]' % value

        return self.execute(Command.FIND_ELEMENT, {
            'using': by,
            'value': value})['value']

遂改成

        self.driver.find_element(
            By.XPATH, "//*[@id=\"app\"]/div/div[1]/div[2]/div[2]/div/form/div[1]/div/div/input").click()
        self.driver.find_element(
            By.XPATH, "//*[@id=\"app\"]/div/div[1]/div[2]/div[2]/div/form/div[1]/div/div/input").clear()
        self.driver.find_element(
            By.XPATH, "//*[@id=\"app\"]/div/div[1]/div[2]/div[2]/div/form/div[1]/div/div/input").send_keys("mayubo")
记得加一下from selenium.webdriver.common.by import By

解决。



这篇关于python3.8+selenium find_element_by_*定位方式语法改变的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程