【求助】touchations,定位web元素时报错“'dict' object has no attribute 'send_keys'”

访问百度首页,定位搜索框,点击搜索按钮,用的是touchations,但是在定位的时候报错“‘dict’ object has no attribute ‘send_keys’”,检查发现这个报错信息是在定义了option之后才会出现。

    def setup(self):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('w3c',False)
        self.driver = webdriver.Chrome(options=option)
        self.driver.implicitly_wait(5)

报错信息
image

完整代码

from selenium import webdriver
from selenium.webdriver import ActionChains, Keys, TouchActions
from selenium.webdriver.common.by import By
from time import sleep


class TestActionChains():
    def setup(self):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('w3c',False)
        self.driver = webdriver.Chrome(options=option)
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()

    def test_touchaction(self):
        """
        实现以下内容:
        打开URL:http://www.baidu.com
        向搜索框中输入“selenium测试”
        通过TouchAction点击搜索框
        滑动到底部,点击下一页
        关闭Chrome
        """
        self.driver.get("https://www.baidu.com/")
        el = self.driver.find_element(By.ID, "kw")
        el_search = self.driver.find_element(By.ID, "su")
        el.send_keys("selenium测试")
        action = TouchActions(self.driver)
        action.tap(el_search)
        action.perform()
        action.scroll_from_element(el, 0, 10000).perform()
        sleep(3)

  • 直接运行你的代码 在send_keys语句是能正常运行的
  • 建议再次尝试运行看看 如果还是报相同的错误的话,尝试在el.send_keys("selenium测试")语句前面加一句print(type(el))看一下el对象的类型是什么
  • 正常情况el对象应该是一个<class 'selenium.webdriver.remote.webelement.WebElement'>类型

selenium 4.0 有兼容性问题 ,尝试在chrome option加入add_experimental_option(‘w3c’, True) ,如果不行执行装3.0的版本