I am trying to automatically login the website using selenium with python. However, I got error as below.
Traceback (most recent call last):
File "C:\Users\KienThong\Automation\Learning\GmailDemo.py", line 15, in <module>
ID = WebDriverWait(driver, 10).until(
File "C:\Python385\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
My code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "_xfUid-1-1643985254"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
The reason why you are having an error is that the ID that you are using is dynamic and changes everytime the page is loaded. Therefore we need to use a static identifier which we can with name="login"
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "login"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
you dont need to tell the driver to wait till the element is present your can simply its xpath as it will executed only after page loads
so use:
ID = driver.find_element(By.XPATH, "/html/body/div[2]/div/div[4]/div/div[2]/div/div/div/div/form/div[1]/div/dl[1]/dd/input")
ID.send_keys("dkthong2010#gmail.com")
using xpath to locating the element is very easy and easy to locate.
just
1.inspect the element
2.right click on the element
3. copy full xpath
To send a character sequence to the Your name or email address field you need 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.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='dismiss-notice'] > span.button-text"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).send_keys("dkthong2010#gmail.com")
Using XPATH:
driver.get('https://www.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'dismiss-notice')]/span[#class='button-text']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='login']"))).send_keys("dkthong2010#gmail.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:
Related
I try to locate elements on a website which are in a form to then send keys to login but I get an error, see below:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="username"]"}
Line of code is:
driver.find_element(By.ID, "username").send_keys("test123")
For HTML code see:
HTML code
I use latest version of Python/Selenium and PyCharm.
I hope someone can help.
I don't see anything wrong in that line of code. Try one of the below
Try applying implicit wait as below:
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(20)
Check if there is a frame/ iframe covering this element, if yes, try below code before locating the element
iframe = driver.find_element_by_xpath("<XPath expression of the frame>")
driver.switch_to.frame(iframe)
To switch back to the main DOM:
driver.switch_to.default_content()
If the frame doesn't exist, then try inducing explicit wait as below:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
Below imports you will require:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Given the HTML:
the element is a <input> element which would accept text input using send_keys() method.
Solution
Ideally to send a character sequence to an <input> element 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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input#username[name='username']"))).send_keys("Coding_89")
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='input' and #id='username'][#name='username']"))).send_keys("Coding_89")
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
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
Trying to update my code to use "driver.find_element(By.XPATH..." instead of "driver.find_elements_by_xpath(...", but I keep getting the following the error when I send keys:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Here is my code:
driver = webdriver.Chrome(PATH)
link_login = "https://www.wyzant.com/tutor/jobs"
driver.get(link_login)
username_input = driver.find_element(By.XPATH, "//*[#id='Username']")[1]
username_input.send_keys("Test")
Use find_elements instead of find_element to select the element like you do in your example:
driver.get('https://www.wyzant.com/tutor/jobs')
username_input = driver.find_elements(By.XPATH, "//*[#id='Username']")[1]
username_input.send_keys("Test")
or change your xpath to select more specific '//form[#class="sso-login-form"]//*[#id="Username"]':
driver.get('https://www.wyzant.com/tutor/jobs')
username_input = driver.find_element(By.XPATH, '//form[#class="sso-login-form"]//*[#id="Username"]')
username_input.send_keys("Test")
Try using more precise XPath locator.
The entire XPath expression should be inside the (By.XPATH, "your_xpath_expression")
You should also use expected conditions explicit waits
This should work:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[#action='/sso/login']//input[#id='Username']"))).send_keys(your_user_name)
You will need to import these imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence within the Username field you need 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.wyzant.com/tutor/jobs")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form.sso-login-form input#Username"))).send_keys("rushi")
Using XPATH:
driver.get("https://www.wyzant.com/tutor/jobs")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[#class='sso-login-form']//input[#id='Username']"))).send_keys("rushi")
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 tried a few websites don't have a problem. "baseURL" tried a lot of time.
driver.find the class element
can't get. Anyone can help?
Click this link below and inside the login page ...
which one to use ??
Code trials:
PATH = "D:\chromedriver.exe"
driver = webdriver.Chrome(PATH)
baseURL = "https://www.kingdoms.com/#logout"
driver = get(baseURL)
print(driver.title)
driver.find_element_by_xpath
There is an nested iframe in that page, so you have to switch to iframe and again to iframe, and then you can send the keys to email address input field.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://www.kingdoms.com/#logout")
wait = WebDriverWait(driver, 20)
try:
wait.until(EC.element_to_be_clickable((By.ID, "cmpbntyestxt"))).click()
except:
pass
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe.mellon-iframe")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='https://mellon-t5.traviangames.com/account/logout']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys('Mario#gmail.com')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))).send_keys("marios password")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='submit']"))).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
The desired fields are within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child 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.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cmpboxbtn.cmpboxbtnyes]"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.mellon-iframe")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://mellon-t5.traviangames.com/account/logout/applicationDomain']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_css_selector("input[name='password']").send_keys("MarioHooi")
Using XPATH:
driver.get('https://www.kingdoms.com/#logout')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='cmpboxbtn cmpboxbtnyes']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='mellon-iframe']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'https://mellon-t5.traviangames.com/account/logout/applicationDomain')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='email']"))).send_keys("Mario#Hooi.com")
driver.find_element_by_xpath("//input[#name='password']").send_keys("MarioHooi")
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:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
How To sign in to Applemusic With Python Using Chrome Driver With Selenium
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