解决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()
    

原文地址:https://www.cnblogs.com/CCX330/p/17434253.html