欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于webdriver定位元素,报错无法点击

程序员文章站 2022-03-24 08:40:06
...
1.换ENTER键
from selenium.webdriver.common.keys import Keys

imageTypeRadio = '//i[starts-with(@class,"input-helper") and contains(.,"%s")]'% image_type
context.driver.find_element_by_xpath(imageTypeRadio).send_keys(Keys.ENTER)

2.元素在页面下方,被遮挡,聚焦到元素后再点击
ERROR Message: unknown error: Element <label>...</label> is not clickable at point (597, 835). Other element would receive the click: <div data-v-d25eeca0="" class="button-box" style="top: 777px;">...</div>

解决方法:聚焦滚动到元素可见,再点击
target = driver.find_element_by_id("id_keypair")
driver.execute_script("arguments[0].scrollIntoView();", target) #拖动到可见的元素去


3.元素被透明遮罩层遮挡,等待遮罩层消失
ERROR Message: unknown error: Element <button class="k-btn" tabindex="0" type="button">...</button> is not clickable at point (811, 352). Other element would receive the click: <div class="ksc-loading-mask"></div>

解决方法:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def wait_loading(context):
    try:
        WebDriverWait(context.driver, 1, 0.1).until(
            EC.visibility_of_element_located((By.XPATH, "//div[@class='ksc-loading-mask']")))
    except TimeoutException:
        pass
    WebDriverWait(context.driver, SMALL_TIME_OUT).until(
        EC.invisibility_of_element_located((By.XPATH, "//div[@class='ksc-loading-mask']")))