【问题标题】:DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead using Selenium in Google Colab弃用警告:不推荐使用 find_elements_by_* 命令。请使用 find_elements() 而不是在 Google Colab 中使用 Selenium
【发布时间】:2022-01-23 20:00:39
【问题描述】:

有一些函数可以与 selenium 一起使用,并且这些函数有一定的输出,但是当我在 google colab 中打开它们时,会得到一些我不想要的输出,这会降低理解。

BETSWITSBOT

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  after removing the cwd from sys.path.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  """
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:6: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:7: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  import sys
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:9: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  if __name__ == '__main__':
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  # Remove the CWD from sys.path while we load stuff.
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:11: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  # This is added back by InteractiveShellApp.init_path()
Hatayspor
Feyenoord
Nice
Aris
Antalyaspor
PSV
Arsenal

在示例中,有没有办法在 'BETSWITHBOT' 和 'HATAYSPOR' 之间获得输出?

我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)

def betswitsbot():
    print("BETSWITSBOT\n")
    driver.get("https://www.betswithbots.com/")
    takimlar = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[3]")
    evTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[4]")
    xTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[5]")
    depTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[6]")
    altTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[7]")
    ustTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[8]")
    ngTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[9]")
    gTahmin = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[10]")

    for x in range(0,len(takimlar)):
        try:
            ev, dep = takimlar[x].text.split(" - ")
            #print(ev, evTahmin[x].text, xTahmin[x].text, depTahmin[x].text, dep, altTahmin[x].text, ustTahmin[x].text, ngTahmin[x].text, gTahmin[x].text, sep="\t")
            if float(evTahmin[x].text) > 50:
                print(ev)
            elif float(depTahmin[x].text) > 50:
                print(dep)
        except:
            a=0

betswitsbot()

【问题讨论】:

  • 如果没有看到您的代码,我们无法猜测您的问题是什么
  • 我添加了代码,在输出部分我不想要“/usr/local...”文本

标签: selenium selenium-webdriver google-colaboratory selenium4 selenium-webdriver-python


【解决方案1】:

这些DeprecationWarning 日志...

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
...
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:5: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
...
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead

...是最新版本 Selenium 更改的结果,它与 Selenium 4 Release Candidate 1 changelog 内联,其中提到:

指定“find_element_by_* ...”警告是弃用警告 (#9700)


解决方案

您必须使用 find_element() 而不是 find_element_by_*。举个例子:

您需要添加以下导入:

from selenium.webdriver.common.by import By
  • 而不是使用:

    takimlar = driver.find_elements_by_xpath("//table[@id='predictions_table']/tbody/tr/td[3]")
    
  • 你需要使用:

    takimlar = driver.find_elements(By.XPATH, "//table[@id='predictions_table']/tbody/tr/td[3]")
    

参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 谢谢,我从没想过硒会发生变化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 2012-05-10
  • 2021-12-20
  • 2020-01-31
  • 1970-01-01
相关资源
最近更新 更多