CSS Selector throws timeout exception in Selenium script - python

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>

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')))

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

Related

Selenium doesn't find element by id (python)

Here is the html element:
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>
my code is:
option = webdriver.ChromeOptions()
option.add_argument("--mute-audio")
browser = webdriver.Chrome("C:/Users/leand/Desktop/kahoot-answer-bot-master/chromedriver.exe",options=option)
browser.get(url + "?launchApp=true")
browser.find_element_by_id('interstitial_join_btn').click()
but I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
...here is some info about the files etc...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="interstitial_join_btn"]"}
(Session info: chrome=89.0.4389.90)
What have I done wrong or is there any other way to click on this element?
It seems synchronization issue.To click Use WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe"))) WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
Import below libraries
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
If above code gives you timeout error then check if that button present inside an iframe or inside #shadowroot element
Update: element is in iframe need to switch.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser=webdriver.Chrome()
browser.get("https://meet37.webex.com/webappng/sites/meet37/meeting/download/7050764c36568ef62dd56dd671cb73c1?launchApp=true")
wait=WebDriverWait(browser,20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[#placeholder='Your full name']"))).send_keys("user1")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[#placeholder='Email address']"))).send_keys("Testuser#gmail.com")
wait.until(EC.element_to_be_clickable((By.ID,"guest_next-btn"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
To select an item on selenium must first be loaded from the browser, so you need to wait some seconds until all html elements is loaded ant then select them
You can do it with the time.sleep() function like this:
time.sleep(2.0) // 2.0 = 2seconds
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>

NoSuchElementException: Message: no such element: Unable to locate element error locating element with Selenium and Python

I have a program which uses the python selenium webdriver and I get the following runtime error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='id_login']"}
(Session info: chrome=83.0.4103.1
HTML
<input type="text" name="login" placeholder="Type your username" required="" id="id_login" xpath="1">16)
code:
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="c:\\Chrome1\chromedriver.exe")
driver.get("https://www.jstor.org")
print(driver.title)
driver.find_element_by_xpath("//a[#class='inline-block plm']").click()
driver.find_element_by_xpath("//input[#id='id_login']").send_keys('xxxxxx#gmail.com')
This error means that selenium could not localize the element because it was not on the site or it did not load. I suggest you using the WebdriverWait() function. It will wait X seconds until the element is clickable. If it will still not be, it will throw an error.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.XPATH, '//a[#class='inline-block plm']')))
More effective if you direct directly to this url:
https://www.jstor.org/action/showLogin?redirectUri=/
And use selenium wait to solve your issue.
driver.get('https://www.jstor.org/action/showLogin?redirectUri=/')
user_name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']")))
user_name.send_keys('xxxxxx#gmail.com')
Although your xpath will work, using an id looks better .element_to_be_clickable((By.ID, "id_login"))
You need following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence to the Username field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.jstor.org/")
ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.inline-block.plm"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys("SunilTirupathi#stackoverflow.com")
Using XPATH:
driver.get("https://www.jstor.org/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='inline-block plm']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']"))).send_keys("SunilTirupathi#stackoverflow.com")
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
Browser Snapshot:
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Selecting xpath - Selenium

I am coding a bot for tinder with selenium but its not working. Here is my code.
fb_button = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fb_button.click()
This code is for clicking the facebook login button. However when i run the code, it dont work healthy.I am getting an error like this.
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate e
lement: {"method":"xpath","selector":"//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div
[2]/div/div/span/div[2]/button"}
However when i try to create an object and try to call this function, it returns something.
HTML of the button:
<button type="button" class="button Lts($ls-s) Z(0) CenterAlign Mx(a) Cur(p) Tt(u) Bdrs(100px) Px(24px) Px(20px)--s Py(0) Mih(42px)--s Mih(50px)--ml button--outline Bdw(2px) Bds(s) Trsdu($fast) Bdc(#fff) C(#fff) Bdc(#fff):h C(#fff):h Bdc(#fff):f C(#fff):f Bdc(#fff):a C(#fff):a Fw($semibold) focus-button-style W(100%) Fz(4vw)--ms" draggable="false" aria-label="Facebook ile oturum aç"><span class="Pos(r) Z(1) D(ib)">Facebook ile oturum aç</span></button>
you can wait until the element can clickable and present in HTML dom.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
fb_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your_xpath")))
fb_button.click()
you can check the wait conditions here!
You can click on the element using xpath:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Facebook ile oturum aç']"))).click()
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

xpath to get a simple div id="xyz" with selenium

the html I'm digging (there is no iframe on it) is:
<div id="app" class="container-fluid">
I try to get the block of the DOM tree corresponding to the above div
this is my code:
from selenium.common.exceptions import NoSuchElementException
try:
app = driver.find_element_by_xpath('//div[#id="app"]')
print(4444444444444444, app)
is_vue_app_loaded = len(app.text) != 0
except NoSuchElementException as e:
print(f'e: {e}')
I get this error:
4444444444444444 <selenium.webdriver.firefox.webelement.FirefoxWebElement
(session="c8da2a89-63fd-4dce-bdb5-28fdf0e67b01",
element="b9be2828-115e-4965-8ebc-b75c09236193")>
e: Message: Unable to locate element: //div[#id='app']
Is there a way to ask the driver to return me the raw html so I can check that the DOM try I think I have is the one I have actualy ?
Check if the element is present inside any iframe.If there you need to switch it first.
If Not then try.
Induce WebDriverWait() and visibility_of_element_located()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#id='app' and #class='container-fluid']")))
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
If your usecase is to return me the raw html of the element you have to induce WebDriverWait for the visibility_of_element_located() inconjunction with get_attribute("outerHTML") and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container-fluid#app"))).get_attribute("outerHTML"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='container-fluid' and #id='app']"))).get_attribute("outerHTML"))
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

I can not find a way to click on a button using Python and Selenium

I have a problem, I happen to have a button with these labels, I'm trying to click through Selenium but I can not find a way to click it. I tried to give it taking as reference the XPath, link text, and CSS selector but I do not achieve my goal. This is the code for the button:
<a class="btn btn-flat pull-right" data-action="export_report"> <i class = "icon-export"> </ i> Export </a>
East of the XPath:
// * [# id = "reports"] / div [1] / div [2] / a
this selector:
#reports> div.span12> div.headline-action-block.pull-right> a
This is the button and my code in Python :(
Button:
My code:
I face this error:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
'value': value})['value']
You can try this:
openrate = driver.find_element_by_css_selector("a.btn")
openrate.click()
Assuming this is the only button, or the first button on the page. Otherwise ("a.btn.btn-flat.pull-right")
As per the HTML you have shared it seems you have invoked click() in the previous step so in this step to click() on the button with text as Export you have to induce WebDriverwait for the element to be clickable and you can use either of the following Locator Strategies :
PARTIAL_LINK_TEXT :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export"))).click()
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-flat.pull-right[data-action='export_report']>i.icon-export"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-flat pull-right' and #data-action='export_report']/i[#class='icon-export']"))).click()

Categories

Resources