CSS选择器在Selenium脚本中引发超时异常

时间:2019-01-31 02:06:03

标签: python selenium css-selectors webdriverwait

  

回溯(最近通话最近):文件   “ C:\ Users \ PSWN672P \ AppData \ Local \ Programs \ Python \ Python37 \ Python   程序\ SNOW1.py”,第17行,在       EC.element_to_be_clickable(((由CSS_SELECTOR,'ul [id * =“ collapseId”]> li:nth-​​child(5)> ul [id * =“ collapseId”]> li> div> a> div> div') )       文件“ C:\ Users \ PSWN672P \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ selenium \ webdriver \ support \ wait.py”,   80行,直到       引发TimeoutException(消息,屏幕,堆栈跟踪)       selenium.common.exceptions.TimeoutException:消息:

尝试使用文本作为我的网上论坛访问元素,并运行脚本以自动单击该元素并导航至下一页:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
import time


browser=webdriver.Ie()
browser.get('http://example.com')

try:

    window_before=browser.window_handles[0]
    element=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'ul[id*="collapseId"]>li:nth-child(5)>ul[id*="collapseId"]>li>div>a>div>div')))
    element.click()
    time.sleep(15)
    window_after=browser.window_handles[1]
    browser.switch_to_window(window_after)

finally:

    browser.quit()

<a class="sn-widget-list-item sn-widget-list-item_hidden-action module-node" id="2ccb50dfc61122820032728dcea648fe" href="task_list.do?sysparm_userpref_module=2ccb50dfc61122820032728dcea648fe&amp;sysparm_query=assignment_group=javascript:getMyGroups()^active=true^assigned_to=^sys_class_name!=cert_follow_on_task^sys_class_name!=sc_req_item^sys_class_name!=sc_request^EQ&amp;sysparm_clear_stack=true" target="gsft_main"><div class="sn-widget-list-content" data-id="2ccb50dfc61122820032728dcea648fe">
    <div class="sn-widget-list-content" data-id="2ccb50dfc61122820032728dcea648fe">
        <div class="sn-widget-list-title">My Groups Work</div>
    </div>
</a>

2 个答案:

答案 0 :(得分:0)

为什么不只按类名查找元素?

element=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CLASS_NAME,'sn-widget-list-title')))

答案 1 :(得分:0)

CSS选择器引发超时异常,因为您改编的Locator Strategy不能唯一地标识所需的元素。 TimeoutException 可能是 ExpectedConditions 失败的结果。 但是,对于文本为“我的网上论坛工作”的元素,click()可以使用以下解决方案:

  • 使用CSS_SELECTOR

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sn-widget-list-item.sn-widget-list-item_hidden-action.module-node[href^='task_list.do?sysparm_userpref_module=']>div.sn-widget-list-content[data-id]>div.sn-widget-list-title")))
    

替代

  • 使用XPATH

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='sn-widget-list-item sn-widget-list-item_hidden-action module-node' and starts-with(@href,'task_list.do?sysparm_userpref_module=')]/div[@class='sn-widget-list-content' and (@data-id)]/div[@class='sn-widget-list-title' and contains(., 'My Groups Work')]")))
    

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC