<i id='Qq9h3'><tr id='Qq9h3'><dt id='Qq9h3'><q id='Qq9h3'><span id='Qq9h3'><b id='Qq9h3'><form id='Qq9h3'><ins id='Qq9h3'></ins><ul id='Qq9h3'></ul><sub id='Qq9h3'></sub></form><legend id='Qq9h3'></legend><bdo id='Qq9h3'><pre id='Qq9h3'><center id='Qq9h3'></center></pre></bdo></b><th id='Qq9h3'></th></span></q></dt></tr></i><div id='Qq9h3'><tfoot id='Qq9h3'></tfoot><dl id='Qq9h3'><fieldset id='Qq9h3'></fieldset></dl></div>

      <small id='Qq9h3'></small><noframes id='Qq9h3'>

        <bdo id='Qq9h3'></bdo><ul id='Qq9h3'></ul>

    1. <legend id='Qq9h3'><style id='Qq9h3'><dir id='Qq9h3'><q id='Qq9h3'></q></dir></style></legend>

        <tfoot id='Qq9h3'></tfoot>

      1. 在 selenium 中不推荐使用 find_element_by_* 命令

        find_element_by_* commands are deprecated in selenium(在 selenium 中不推荐使用 find_element_by_* 命令)
          <tbody id='naWuH'></tbody>

                <bdo id='naWuH'></bdo><ul id='naWuH'></ul>
              • <tfoot id='naWuH'></tfoot>
              • <small id='naWuH'></small><noframes id='naWuH'>

                <i id='naWuH'><tr id='naWuH'><dt id='naWuH'><q id='naWuH'><span id='naWuH'><b id='naWuH'><form id='naWuH'><ins id='naWuH'></ins><ul id='naWuH'></ul><sub id='naWuH'></sub></form><legend id='naWuH'></legend><bdo id='naWuH'><pre id='naWuH'><center id='naWuH'></center></pre></bdo></b><th id='naWuH'></th></span></q></dt></tr></i><div id='naWuH'><tfoot id='naWuH'></tfoot><dl id='naWuH'><fieldset id='naWuH'></fieldset></dl></div>
                  <legend id='naWuH'><style id='naWuH'><dir id='naWuH'><q id='naWuH'></q></dir></style></legend>
                  本文介绍了在 selenium 中不推荐使用 find_element_by_* 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  函数启动时

                  def 运行(驱动程序路径):driver = webdriver.Chrome(executable_path=driver_path)driver.get('https://tproger.ru/quiz/real-programmer/')button = driver.find_element_by_class_name("quiz_button")按钮.click()运行(驱动程序路径)

                  我收到如下错误:

                  <ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service objectdriver = webdriver.Chrome(executable_path=driver_path)<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* 命令已弃用.请改用 find_element()button = driver.find_element_by_class_name("quiz_button")

                  ...但我不明白为什么.

                  我正在为我的 chrome 版本使用最新版本的 webdriver.我不明白我为什么会得到

                  `find_element_by_* 命令已弃用`

                  ...当该命令存在于文档中时.

                  解决方案

                  这个错误信息...

                  DeprecationWarning: find_element_by_* 命令已弃用.请改用 find_element()

                  ...暗示 find_element_by_* 命令在最新的 Selenium Python 库中已弃用.

                  作为@AutomatedTester 提及:这个DeprecationWarning 反映了对简化跨语言 API 的决定所做的更改,而这正是这样做的.


                  解决方案

                  您必须使用 find_element().举个例子:

                  <块引用><块引用>

                  您必须包含以下导入

                  from selenium.webdriver.common.by import By

                  • 使用 class_name:

                    button = driver.find_element_by_class_name("quiz_button")

                    需要替换为:

                    button = driver.find_element(By.CLASS_NAME, "quiz_button")


                  在类似的行中,您还必须更改以下内容:

                  • 使用id:

                    element = find_element_by_id("element_id")

                    需要替换为:

                    element = driver.find_element(By.ID, "element_id")

                  • 使用名称:

                    element = find_element_by_name("element_name")

                    需要替换为:

                    element = driver.find_element(By.NAME, "element_name")

                  • 使用 link_text:

                    element = find_element_by_link_text("element_link_text")

                    需要替换为:

                    element = driver.find_element(By.LINK_TEXT, "element_link_text")

                  • 使用 partial_link_text:

                    element = find_element_by_partial_link_text("element_partial_link_text")

                    需要替换为:

                    element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")

                  • 使用 tag_name:

                    element = find_element_by_tag_name("element_tag_name")

                    需要替换为:

                    element = driver.find_element(By.TAG_NAME, "element_tag_name")

                  • 使用 css_selector:

                    element = find_element_by_css_selector("element_css_selector")

                    需要替换为:

                    element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")

                  • 使用 xpath:

                    element = find_element_by_xpath("element_xpath")

                    需要替换为:

                    element = driver.find_element(By.XPATH, "element_xpath")

                  When starting the function

                  def run(driver_path):    
                      driver = webdriver.Chrome(executable_path=driver_path)    
                      driver.get('https://tproger.ru/quiz/real-programmer/')    
                      button = driver.find_element_by_class_name("quiz_button")
                      button.click()
                  run(driver_path)
                  

                  I'm getting errors like these:

                  <ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
                    driver = webdriver.Chrome(executable_path=driver_path)
                  <ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
                    button = driver.find_element_by_class_name("quiz_button")
                  

                  ... but I can't understand why.

                  I'm using webdriver at the latest version for my chrome's version. I don't why I get

                  `find_element_by_* commands are deprecated` 
                  

                  ... when it's in the documentation that the command exists.

                  解决方案

                  This error message...

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

                  ...implies that the find_element_by_* commands are deprecated in the latest Selenium Python libraries.

                  As @AutomatedTester mentions: This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages and this does that.


                  Solution

                  Instead you have to use find_element(). As an example:

                  You have to include the following imports

                  from selenium.webdriver.common.by import By
                  

                  • Using class_name:

                    button = driver.find_element_by_class_name("quiz_button")
                    

                    Needs be replaced with:

                    button = driver.find_element(By.CLASS_NAME, "quiz_button")
                    


                  In similar lines you also have to change the following:

                  • Using id:

                    element = find_element_by_id("element_id")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.ID, "element_id")
                    

                  • Using name:

                    element = find_element_by_name("element_name")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.NAME, "element_name")
                    

                  • Using link_text:

                    element = find_element_by_link_text("element_link_text")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.LINK_TEXT, "element_link_text")
                    

                  • Using partial_link_text:

                    element = find_element_by_partial_link_text("element_partial_link_text")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
                    

                  • Using tag_name:

                    element = find_element_by_tag_name("element_tag_name")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.TAG_NAME, "element_tag_name")
                    

                  • Using css_selector:

                    element = find_element_by_css_selector("element_css_selector")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
                    

                  • Using xpath:

                    element = find_element_by_xpath("element_xpath")
                    

                    Needs be replaced with:

                    element = driver.find_element(By.XPATH, "element_xpath")
                    

                  这篇关于在 selenium 中不推荐使用 find_element_by_* 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <legend id='W652Y'><style id='W652Y'><dir id='W652Y'><q id='W652Y'></q></dir></style></legend>
                • <i id='W652Y'><tr id='W652Y'><dt id='W652Y'><q id='W652Y'><span id='W652Y'><b id='W652Y'><form id='W652Y'><ins id='W652Y'></ins><ul id='W652Y'></ul><sub id='W652Y'></sub></form><legend id='W652Y'></legend><bdo id='W652Y'><pre id='W652Y'><center id='W652Y'></center></pre></bdo></b><th id='W652Y'></th></span></q></dt></tr></i><div id='W652Y'><tfoot id='W652Y'></tfoot><dl id='W652Y'><fieldset id='W652Y'></fieldset></dl></div>

                    <tfoot id='W652Y'></tfoot>

                          <bdo id='W652Y'></bdo><ul id='W652Y'></ul>
                            <tbody id='W652Y'></tbody>

                            <small id='W652Y'></small><noframes id='W652Y'>