Solve the problem that elements cannot be clicked in Selenium. Selemium uses JS code driver.execute_script to click elements.

@FindBy(how = How.XPATH, using = "//*[text()='A1. Approved']")
private WebElement Approved;

driver.execute_script("arguments[0].click();",Approved)


What this sentence means is to use JavaScript to perform a click operation in the browser and click the page element "supportUser".

Specifically, "driver.execute_script" is a method that calls the browser driver (driver), which can be used to execute JavaScript code in the browser. "arguments[0].click()" is a piece of JavaScript code, which means calling the "click" method of the first parameter (ie "supportUser"), so that you can click on this element in the browser.

In general, the function of this sentence is to automatically perform click operations in the browser through the browser driver.

If the code reports an error: selenium.common.exceptions.InvalidArgumentException: Message: invalid argument

This may be due to the way elements interact or the special design of the page. In order to solve this problem,
we can try the following three solutions: simulating mouse operations, simulating keyboard operations, and operating through JavaScript.

1. Simulate mouse operations

When an element cannot be clicked through the click method, we can simulate mouse operations through Selenium's ActionChains class.

Operation steps:
# 1. Import the ActionChinas class
from selenium.webdriver import ActionChains
# 2. Instantiate the ActionChinas object
actions = ActionChains(driver)
# 3. Perform mouse operations, such as clicking on the element
actions.click(ele).perform()

Code optimization

import time
from selenium import webdriver
from pywinauto.keyboard import send_keys
# 1. Import ActionChinas class
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. Instantiate the ActionChinas object
    actions = ActionChains(dr)
    # 3. Perform mouse operations, such as clicking on the element
    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. Simulate keyboard operation

If the element cannot be clicked by the mouse, we can try to use keyboard operations to trigger the corresponding event. Specific steps are as follows:

Guide package

from selenium.webdriver.common.keys import Keys
sends specific keyboard keys on the element

element.send_keys(Keys.ENTER)# Simulate the Enter key

3. Operation through js


If elements cannot be clicked through mouse and keyboard operations, we can try to use JavaScript to directly manipulate page elements. Specific steps are as follows:

Use execute_script method to execute js code

driver.execute_script("arguments[0].click();", element)
Baidu image search code optimization

import time
from selenium import webdriver
from pywinauto.keyboard import send_keys
# 1. Import ActionChinas class
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"]')
    # #Use execte_script to click. Because it is impossible to click
    dr.execute_script("arguments[0].click();",ele) through click()
    actions.click(ele).perform()
    time.sleep(3)
    send_keys(keys=r'D:\ api_test.

    time.sleep(30)
except Exception as e:
    raise e
finally:
    dr.quit()

Guess you like

Origin blog.csdn.net/qq_30273575/article/details/133134313