1. Selenium无法点击元素,报错:ElementClickInterceptedException:element click intercepted
解决办法:文章来源地址https://www.toymoban.com/news/detail-547263.html
- 方法一:
element = driver.find_element_by_xpath("表达式") driver.execute_script("arguments[0].click();", element)
- 方法二:
element = driver.find_element_by_xpath('表达式') webdriver.ActionChains(driver).move_to_element(element ).click(element ).perform()
2. selenium操作下拉滚动条方法
- 方法一 使用js脚本直接操作:
js = "var q=document.getElementById('id').scrollTop=10000" driver.execute_script(js)
- 方法二 使用JavaScript脚本将滚动条拖动到指定地方:
target = driver.find_element_by_id("id_keypair") # 需要将滚动条拖动至的指定的元素对象定位 driver.execute_script("arguments[0].scrollIntoView();", target) # 将滚动条拖动到元素可见的地方
- 方法三 根据页面显示进行变通(在本实例中的页面中,密码是输入框,正常手工操作时,可以通过tab键可以从用户框切换到密码框中,所以根据此思路,在python中也可以发送tab键来切换,使元素显示。):
from selenium.webdriver.common.keys import Keys # 导入Keys类 driver.find_element_by_id("id_login_method_0").send_keys(Keys.TAB) # 定位元素并操作输入
- 方法四 send_keys(Keys.END) 模拟向页面发送空格键:
注意: 发送空格键的元素应该是整个页面对象,比如说定位到页面body后进行操作
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400,900)
import time
def search():
try:
browser.get("https://www.taobao.com")
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
for i in range(5):
browser.find_element_by_tag_name('body').send_keys(Keys.Space)
time.sleep(1)
except TimeoutException:
search()
search()
- 方法五 使用鼠标操作:
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
browser=webdriver.Chrome("G:/dj/chromedriver.exe")
wait=WebDriverWait(browser,10)
browser.set_window_size(1400,900)
import time
def search():
try:
browser.get("https://www.taobao.com")
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"body > div:nth-child(29)")))
target = browser.find_element_by_css_selector('body > div:nth-child(29)')
actions = ActionChains(browser)
actions.move_to_element(target)
actions.perform()
except TimeoutException:
search()
search()
3. 等待元素加载
#创建WebDriverWait对象
wait = WebDriverWait(browser, 10)
wait.until(expected_conditions.visibility_of_element_located((By.XPATH, 'elemental')))
4. 缩放页面
script = "document.body.style.zoom='75%'"
driver.execute_script(script)
文章来源:https://www.toymoban.com/news/detail-547263.html
到了这里,关于selenium常见问题(网页缩放、滑动,元素获取不到......)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!