I have to automate account creation for few "Senior Citizen" that live in nursing home. (for free, I'm volunteer)
I'm figuring out how to click on button like this :
<button type="submit" data-uid="id-3" role="button" class="_2CEO5 _2eWEL _3cHWt _2Jtx4 V9Kxm _1s9tX _2eWEL _3cHWt _2eWEL _3cHWt _2Jtx4 _3cHWt _5Qrl3">
<span class="_100nC"><div class="_2EteE _2WXqV _34xiU _1TcN5 _2WXqV _34xiU" style="border-top-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255);"></div></span><span class="_2HwQ3">Create an Account</span></button>
I tried with
on chrome browser , righ-click > copy Xpath
driver.find_element_by_xpath('//*[#id="plex"]/div/div/div/div[1]/form/button')
but the Selenium cant find it
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="plex"]/div/div/div/div[1]/form/button"}
How to click ?
Thank for any hints
The url is: https://www.plex.tv/sign-up/
Navigating directly to their login auth iframe link
from selenium import webdriver
driver = webdriver.Chrome(executable_path=path)
driver.implicitly_wait(10)
driver.get('https://www.plex.tv/sign-up/')
#accept cookies
driver.find_element_by_xpath('/html/body/div[8]/div/div/div[3]/a[2]').click()
#navigate to iframe src
driver.get(driver.find_element_by_id("fedauth-iFrame").get_attribute('src'))
#input email
driver.find_element_by_xpath('//*[#id="email"]').send_keys("email#email.com")
#input password
driver.find_element_by_xpath('//*[#id="password"]').send_keys('password')
#submit
driver.find_element_by_xpath('//*[#id="plex"]/div/div/div/div[1]/form/button/span[2]').click()
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.Firefox()
driver.get("https://www.plex.tv/sign-up/")
driver.maximize_window()
try:
driver.find_element_by_xpath("/html/body/div[2]/div[3]/a[1]").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[4]/div/a").click()
driver.find_element_by_xpath("/html/body/div[9]/div/div/div/div[5]/a").click()
except Exception as e:
print("Nothing to click before submission!")
iframe_el = driver.find_element(By.ID, "fedauth-iFrame")
# iframe_el.get_attribute("src")
try:
driver.get(iframe_el.get_attribute("src"))
# fill the form
element = (
WebDriverWait(driver, 10)
.until(
EC.presence_of_element_located(
(By.XPATH, "/html/body/div[1]/div/div/div/div[1]/form/button")
)
)
.click()
)
print("button is found")
except Exception as e:
print("no element")
finally:
driver.quit()
Instead of targetting the parent BUTTON target the inner SPAN inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit'][data-uid^='id'] span:nth-of-type(2)"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#type='submit' and starts-with(#data-uid, 'id')]//span[text()='Create an Account']"))).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
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
Related
Here is the full code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(executable_path = r"C:\Users\H\Desktop\Automated_Tasks\msedgedriver.exe") # Modify the path here...
# Navigate to URL
driver.get("https://powerusers.microsoft.com/t5/notificationfeed/page")
#accept cookies
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/center/div[1]/div/div[2]/button[1]"))).click()
#click button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).click()
The following line:
button=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "viewMoreLink"))).click()
gives an error:
File C:\Program Files\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:249 in check_response
raise exception_class(message, screen, stacktrace)
ElementClickInterceptedException: element click intercepted: Element is not clickable at point (476, 1865)
(Session info: MicrosoftEdge=109.0.1518.61)
Here is the HTML for the button I'm trying to click.
<a onclick="return LITHIUM.EarlyEventCapture(this, 'click', true)" class="lia-button lia-button-primary view-more-icon lia-link-ticket-post-action" data-lia-action-token="gY01pJ4IhqNcqA8Ouq1d20HgZFI9CVTHrEYgxObqxWantjAxFsOxTacdu8LdHjd0" rel="nofollow" id="viewMoreLink" href="https://powerusers.microsoft.com/t5/notificationfeed/page.notificationlist.notificationlistitems.viewmorelink:viewmore/notificationfeed.lastLoadedTimestamp/1674422253100/notificationfeed.lastViewedTimestamp/1674505573710/container_element/notificationList"><span>Show more</span></a>
Here is a picture showing where the desired element is in:
As per the given HTML to click on the element with text as Show more you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.lia-button.lia-button-primary.view-more-icon.lia-link-ticket-post-action#viewMoreLink[href$='notificationList'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).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
You can find a relevant detailed discussion in ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python
I've already tried in several ways to click this checkbox with selenium and I couldn't.
Link: http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira
I already tried with XPATH, with CLASS_NAME and others, but the return is always the same:
no such element: Unable to locate element:
Can anyone help me?
Code trials:
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from capmonster_python import RecaptchaV2Task
class ChromeAuto:
def __init__(self):
options = Options()
ua = UserAgent()
self.userAgent = ua.random
print(self.userAgent)
options.add_argument(f'user-agent={self.userAgent}')
self.driver_path = r'chromedriver'
self.options = webdriver.ChromeOptions()
self.options.add_argument('--profile-directory=1')
self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
self.options.add_experimental_option("useAutomationExtension", False)
self.captcha = RecaptchaV2Task("c6eea325a7a78273c062d2bb23a2a43d")
self.chrome = webdriver.Chrome(
self.driver_path,
options=self.options
)
def test(self):
self.chrome.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
sleep(5)
self.chrome.find_element(By.XPATH, '//*[#id="recaptcha-anchor"]/div[1]').click()
if __name__ == '__main__':
chrome = ChromeAuto()
chrome.test()
The ReCaptcha checkbox is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='reCAPTCHA']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
Using XPATH:
driver.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='reCAPTCHA']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
PS: Clicking on the ReCaptcha checkbox opens the image selection panel.
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:
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
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
No matter how long I wait, it seems selenium can't find the "watch_online" button.
I've tried both by XPath, full XPath, and CSS selector.
I want to get the href link from the "Watch Online" button.
import os
import glob
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
# Create local browser cache
browser_data_location = "browser_data"
options = webdriver.ChromeOptions()
options.add_argument(f'user-data-dir={os.getcwd()}/{browser_data_location}')
i_m_not_a_robot_xpath = '//*[#id="landing"]/div[2]/center/img'
generate_link_xpath = '//*[#id="generater"]/img'
click_to_continue = '//*[#id="showlink"]'
get_download_link = '/html/body/section/div/div/div/center/a'
watch_online = '//*[#id="download-hidden"]/a'
with webdriver.Chrome(options=options, ) as driver:
wait = WebDriverWait(driver, 10)
time.sleep(2)
driver.get(
"https://www.rtilinks.com/?82255aba71=RmwzVDZObDFBdDQvay8zRjhiaStoM004Ymd1T201MnBQelJpdW5oK1UxeGFvbFZUY1FEVXMrY0o2UnhqeGxOOFlwN3JlUElad2h0ek9pQ1ZFZndXSG9UTzA1aFpmTEhoanBVUldEYWwwWVU9")
# wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, upload_box_css))).send_keys(file)
wait.until(ec.element_to_be_clickable((By.XPATH, i_m_not_a_robot_xpath))).click()
# time.sleep(1)
wait.until(ec.element_to_be_clickable((By.XPATH, generate_link_xpath))).click()
wait.until(ec.element_to_be_clickable((By.XPATH, click_to_continue))).click()
# original_window = driver.current_window_handle
driver.close()
driver.switch_to.window(driver.window_handles[0])
wait.until(ec.element_to_be_clickable((By.XPATH, get_download_link))).click()
time.sleep(2)
link = driver.find_element(By.XPATH, watch_online)
print(link.get_attribute('href'))
The element Watch Online is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Watch Online"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://purefiles.in']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.is-success"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='button is-success' and contains(., 'Watch Online')]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://purefiles.in')]")))
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
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
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
I am trying to click a link on a web page with Python Selenium but I am getting this exception:
no such element: Unable to locate element:
I have already tried using find_element_by_xpath, find_element_by_partial_link_text and find_element_by_link_text.
This is my code:
import time
from selenium import webdriver
driver = webdriver.Chrome('C:/Users/me/Downloads/projetos/chromedriver_win32/chromedriver.exe') # Optional argument, if not specified will search path.
driver.get('http://10.7.0.4/web/guest/br/websys/webArch/mainFrame.cgi');
time.sleep(10) # Let the user actually see something!
#elem = driver.find_element_by_xpath('//*[#id="machine"]/div[1]/div[1]/dl[2]/dt/a')
elem = driver.find_element_by_link_text('Mensagens (2item(ns))')
elem.click()
print("Fim...")
This is the element I need to click:
Mensagens (2item(ns))
You can try with explicit waits and with the customized css :
CSS_SELECTOR :
a[href*='../../websys/webArch/getStatus.cgi']
Sample code :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='../../websys/webArch/getStatus.cgi']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update 1 :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'ebsys/webArch/getStatus.cgi') and contains(text(), 'Mensagens')]"))).click()
When i try to locate one element with selellium it fails
driver = webdriver.Chrome(executable_path = r'./chromedriver.exe')
driver.get("http://eltiempo.com/login")
try:
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, "//*[#id='username']"))
)
finally:
driver.quit()
To send a character sequence to the username field as the element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.eltiempo.com/login")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Nick Rondon#stackoverflow.com")
Using XPATH:
driver.get('https://www.eltiempo.com/login')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']"))).send_keys("Nick Rondon#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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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