解决Selenium中无法点击元素方案

解决Selenium中无法点击元素

在使用Selenium进行Web自动化测试时,我们经常会遇到一些无法通过click方法点击元素的情况。

比如:我要在百度上传图片并搜索时,通过click来点击元素时无法点击

image-20230526090745728

import time
from selenium import webdriver
from pywinauto.keyboard import send_keys
try:
    dr = webdriver.Chrome()
    dr.get("https://www.baidu.com")
    dr.implicitly_wait(5)
    dr.find_element_by_xpath('//span[@class="soutu-btn"]').click()
    ele = dr.find_element_by_xpath('//div[@class="upload-wrap"]/input[@type="file"]')
    ele.click()
    time.sleep(3)
    send_keys(keys=r'D:\api_test.jpg')
    send_keys(keys='{ENTER}')
    time.sleep(30)
except Exception as e:
    raise e
finally:
    dr.quit()

上述代码会报错:selenium.common.exceptions.InvalidArgumentException: Message: invalid argument

这可能是由于元素的交互方式或页面的特殊设计导致的。为了解决这个问题,
我们可以尝试以下三种方案:模拟鼠标操作、模拟键盘操作和通过JavaScript操作。

1. 模拟鼠标操作

当无法通过click方法点击元素时,我们可以通过Selenium的ActionChains类来模拟鼠标操作,

  1. 操作步骤:
# 1.导入ActionChinas类
from selenium.webdriver import ActionChains
# 2.实例化ActionChinas对象
actions = ActionChains(driver)
# 3.执行鼠标操作,如点击元素
actions.click(ele).perform()
  1. 代码优化

    import time
    from selenium import webdriver
    from pywinauto.keyboard import send_keys
    # 1.导入ActionChinas类
    from selenium.webdriver import ActionChains
    try:
        dr = webdriver.Chrome()
        dr.get("https://www.baidu.com")
        dr.implicitly_wait(5)
        dr.find_element_by_xpath('//span[@class="soutu-btn"]').click()
        ele = dr.find_element_by_xpath('//div[@class="upload-wrap"]/input[@type="file"]')
        # 2.实例化ActionChinas对象
    	actions = ActionChains(dr)
    	# 3.执行鼠标操作,如点击元素
        actions.click(ele).perform()
        time.sleep(3)
        send_keys(keys=r'D:\api_test.jpg')
        send_keys(keys='{ENTER}')
        time.sleep(30)
    except Exception as e:
        raise e
    finally:
        dr.quit()
    

2. 模拟键盘操作

如果元素无法通过鼠标点击,我们可以尝试使用键盘操作来触发相应的事件。具体步骤如下:

  1. 导包

    from selenium.webdriver.common.keys import Keys
    
  2. 在元素发送特定的键盘按键

    element.send_keys(Keys.ENTER)# 模拟回车键
    

3. 通过js操作

如果无法通过鼠标和键盘操作来点击元素,我们可以尝试使用JavaScript来直接操作页面元素。具体步骤如下:

  1. 使用execute_script方法来执行js代码

    driver.execute_script("arguments[0].click();", element)
    
  2. 百度搜图代码优化

    import time
    from selenium import webdriver
    from pywinauto.keyboard import send_keys
    # 1.导入ActionChinas类
    from selenium.webdriver import ActionChains
    try:
        dr = webdriver.Chrome()
        dr.get("https://www.baidu.com")
        dr.implicitly_wait(5)
        dr.find_element_by_xpath('//span[@class="soutu-btn"]').click()
        ele = dr.find_element_by_xpath('//div[@class="upload-wrap"]/input[@type="file"]')
        # #使用execte_script来来点击。因为无法通过click()点击
        dr.execute_script("arguments[0].click();",ele)
        actions.click(ele).perform()
        time.sleep(3)
        send_keys(keys=r'D:\api_test.jpg')
        send_keys(keys='{ENTER}')
        time.sleep(30)
    except Exception as e:
        raise e
    finally:
        dr.quit()
    
posted @   CCX330  阅读(1100)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示