This is a weird issue I have ran into and I can't find any solution for this across the internet. I was using selenium in google colab to scrape a website and my code was working completely fine. I woke up the next day and ran the code again without changing a single line and don't know how/why my code starting giving me this error, AttributeError: 'WebDriver' object has no attribute 'find_element_by_link_text'. Same for find_element_by_class_name and id etc. I then rechecked a previously working script just to confirm and that gave me the same error too. I am confused about what happened suddenly and the scripts started giving me these errors.
这是我遇到的一个奇怪的问题,我在互联网上找不到任何解决方案。我在谷歌colab中使用selenium来抓取一个网站,我的代码运行得很好。第二天醒来,我再次运行代码,没有更改任何一行,也不知道我的代码是如何/为什么开始出现这个错误的,AttributeError:“WebDriver”对象没有属性“find_element_by_link_text”。find_element_by_class_name和id等也是如此。然后我重新检查了以前工作的脚本以进行确认,这也给了我同样的错误。我对突然发生的事情感到困惑,脚本开始给我这些错误。
How do I solve this? What am I doing wrong here?
我该如何解决此问题?我在这里做错了什么?
!pip install selenium
!apt-get update
!apt install chromium-chromedriver
from selenium import webdriver
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',options=chrome_options)
driver.get("https://petrowiki.spe.org/PetroWiki")
driver.title #this line is returning the correct title value, code is able to access the url
peh = driver.find_element_by_link_text('Pet. Eng. Handbook')
peh.click()
更多回答
优秀答案推荐
Selenium just removed that method in version 4.3.0
. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium刚刚在4.3.0版本中删除了该方法。请参阅变更:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout
You now need to use:
您现在需要使用:
driver.find_element("link text", "Pet. Eng. Handbook")
For improved reliability, you should consider using WebDriverWait
in combination with element_to_be_clickable
.
为了提高可靠性,您应该考虑将WebDriverWait与element_to_be_clickleable结合使用。
更多回答
Goodness! I knew this had something to do with version and checked selenium github yesterday but couldn't find my answer. Thanks a lot! Appreciate it.
善良我知道这和版本有关,昨天检查了selenium github,但找不到答案。非常感谢!很感激。
OP sure the new code is correct? I think it would need to be driver.find_element(By.LINK_TEXT, 'Pet. Eng. Handbook')
OP确定新代码是正确的吗?我认为它应该是driver.find_element(By.LINK_TEXT,“宠物工程手册”)
我是一名优秀的程序员,十分优秀!