Selenium doesn't find element by id (python) - 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>

Related

ElementNotInteractableException: Message: Element is not reachable by keyboard

I have the following snippet of JS code in a form that I want to use Selenium to input:
<oj-input-password id="idcs-signin-basic-signin-form-password" data-idcs-placeholder-translation-id="idcs-password-placeholder" class="oj-sm-12 oj-inputpassword oj-form-control oj-component oj-complete" value="{{password}}" placeholder="[[bundle('signin.password')]]" labelled-by="ui-id-2"><input data-oj-internal="" type="password" placeholder="Password" class="oj-inputpassword-input oj-component-initnode" id="idcs-signin-basic-signin-form-password|input"></oj-input-password>
I'm trying to enter the password:
password = browser.find_element_by_id('idcs-signin-basic-signin-form-password')
password.send_keys("my_password")
But then I get the following error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <oj-input-password id="idcs-signin-basic-signin-form-password" class="oj-sm-12 oj-inputpassword oj-form-control oj-component oj-complete"> is not reachable by keyboard
Why does this happen and what's the workaround?
Import explicit wait:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Use it this way:
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "#idcs-signin-basic-signin-form-password")))
And use CSS selector instead of id:
password = browser.find_element_by_css_selector("#idcs-signin-basic-signin-form-password")
password.send_keys("my_password")
# indicates an element id.
Also, check for the conditions:
element is not iframe
element is not in shadow DOM.
I would suggest you to use ActionChains
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.find_elements_by_css_selector("input[type='password'][placeholder='Password']")).send_keys('your password').perform()
also make sure to launch browser in full screen mode. and if scrolling is required, I would suggest you to do that as well.

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

How to wait till page loads and then press the load more button in selenium?

Using selenium to load and page and need to click load more button, but after it clicks load more than 100 times
i m getting this error of element click intercepted. because after 100 times the page takes time to load. and code doesnt know where to click.
Tried increasing sleep time to 20 seconds also but at some points if page takes more than 20 seconds the code returns error
error :
ElementNotInteractableException: Message: element not interactable (Session info: chrome=75.0.3770.100)
code :
from selenium import webdriver
import time
import pandas as pd
driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
click_more=True
while click_more:
time.sleep(2)
driver.find_element_by_css_selector(".btn.btn-primary.btn-lg").click();
Consider introducing Explicit Wait to ensure that the button is there prior to attempting clicking it
Example code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
click_more=True
while click_more:
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn.btn-primary.btn-lg"))).click()
More information:
Python Selenium: Wait Support
Python Selenium: Waits
How to use Selenium to test web applications using AJAX technology
In these situations I typically stop the page load, perhaps scroll a couple of times and use screen coordinates with a macro tool like AppRobotic to click the button. Something like this should work for you:
import win32com.client
from win32com.client import Dispatch
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
# wait 20 seconds per question
x.Wait(20000)
# scroll down a couple of times in case page javascript is waiting for user interaction
x.Type("{DOWN}")
x.Wait(2000)
x.Type("{DOWN}")
x.Wait(2000)
# forcefully stop pageload at this point
driver.execute_script("window.stop();")
# if clicking with Selenium still does not work here, use screen coordinates
x.MoveCursor(xCoord, yCoord)
x.MouseLeftClick
x.Wait(2000)
It would appear that you might be running into the google ad at the bottom of the page.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome()
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
click_more=True
wait = WebDriverWait(driver, 30)
while click_more:
try:
elem = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[#class="btn btn-primary btn-lg"]')),
"Cannot find 'Load More' button")
elem.click()
except TimeoutException:
click_more = False
Using xpath and visibility_of_element_located I get the following exception:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="btn btn-primary btn-lg">...</button> is not clickable at point (591, 797). Other element would receive the click: <iframe id="google_ads_iframe_/21692090825/ST_FlexFooter_0" title="3rd party ad content" name="google_ads_iframe_/21692090825/ST_FlexFooter_0" width="970" height="90" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" srcdoc="" style="border: 0px; vertical-align: bottom;" data-google-container-id="1" data-load-complete="true"></iframe>
You either need to close the google add or scroll the page down a little before attempting to click the button. Once that is done, the while loop should work.
Try the script below to keep clicking on that button until there is none left to click.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver, 10)
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
while True:
try:
item = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#load_button > button")))
except TimeoutException:
item = ""
if not item: break
driver.execute_script("arguments[0].scrollIntoView();", item)
item.click()

CSS Selector throws timeout exception in Selenium script

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

How to reach inside <main> tags using selenium Python?

I am using Python 2.7.12 and Selenium 3.0.2.
I wanted to find a tag inside tag <section>, here is the code:
driver = webdriver.Chrome()
driver.get("https://www.semanticscholar.org/")
input_t = driver.find_element_by_xpath('//input[#type="search"]')
input_t.send_keys(keyword)
input_t.send_keys(Keys.ENTER)
target = driver.find_element_by_xpath('//main')
Running this, I got an exception:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//main"}
But actually, there is indeed a tag in the page:
<main class="main-column results" data-reactid=".dyth4mk2kg.0.1.0.1"><div class="controls" data-reactid=".dyth4mk2kg.0.1.0.1.1">
...
</main>
It's just timing issue. You should try using Explicit Waits to wait until main tag loaded and visible after clicking on search button as below :-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.semanticscholar.org/")
#Now enter text to search
driver.find_element_by_name("q").send_keys(keyword)
#Now click on search button
driver.find_element_by_css_selector(".search-bar .button").click()
#Now wait until main tag visible
target = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "main.main-column.results")))

Categories

Resources