Reputation: 11
Traceback (most recent call last):File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\Python programs\SNOW1.py", line 17, in EC.element_to_be_clickable((By.CSS_SELECTOR,'ul[id*="collapseId"]>li:nth-child(5)>ul[id*="collapseId"]>li>div>a>div>div')) File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
Trying to access the element with text as My Groups Work and run a script to automatically click that element and navigate to next page:
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&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&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>
Upvotes: 1
Views: 1223
Reputation: 193058
CSS Selector throws timeout exception as the Locator Strategy which you have adapted doesn't identifies the desired element uniquely. Perhaps TimeoutException is the outcome of failed ExpectedConditions.
However, to click()
on the element with text as "My Groups Work" you can use the following solution:
Using 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")))
Alternative
Using 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')]")))
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 0
Reputation: 337
why not just locate element by class name?
element=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CLASS_NAME,'sn-widget-list-title')))
Upvotes: 0