Selenium2+python automation 46-js solves the problem of click failure

foreword

Sometimes the element has been found, no error is reported when running, and nothing happens on the page after clicking. This kind of problem is encountered, and it is a headache, because there is no error, but the click event is invalid.

This article uses 2 methods to solve this weird click event failure problem

1. Problems encountered

1. When practicing Baidu's search settings button, click the save settings button, the alert pops up but does not pop up (the code does not report an error, but it just fails to get the alert), I believe I am not the only one who has encountered it.

 

2. Click on the parent element

1. When encountering this problem, it should be the sequelae caused by the previous operation of select (because I commented out the select paragraph, it can be clicked successfully)

2. The first solution is to click on its parent element once, and then click on the element

3. The implementation code is as follows

 

3. js click directly

1. When you encounter this kind of weird problem, it's time to make a trick: js Dafa

2. Execute the click event directly with js

 

4. Reference code

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Firefox()
url = "https://www.baidu.com"
driver.get(url)
time.sleep(3)
mouse = driver.find_element("link text", "设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(3)
driver.find_element("link text", "搜索设置").click()
time.sleep(3)
s = driver.find_element("id", "nr")
Select(s).select_by_visible_text("每页显示50条")

# 方法一:先点父元素 交流QQ群:232607095
# driver.find_element("id", "gxszButton").click()
# driver.find_element("class name", "prefpanelgo").click()

# Method 2: Use js to click directly on the communication QQ group: 232607095
js = 'document.getElementsByClassName("prefpanelgo")[0].click() ;'
driver.execute_script(js)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348231&siteId=291194637