当前位置:   article > 正文

selenium find_element_by_定位方式语法改变,提示find_element_by_ commands are deprecated

selenium find_element_by_定位方式语法改变,提示find_element_by_ commands are deprecated

上周重装系统升级了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']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

遂改成

        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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

记得加一下from selenium.webdriver.common.by import By

解决。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/690717
推荐阅读
相关标签
  

闽ICP备14008679号