How to send keys with python selenium after an element has loaded? - python

My code:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as when
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
driver.get("https://classroom.google.com")
element = driver.find_element(By.XPATH, '//*[#id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a')
element.click()
wait = webdriver.support.ui.WebDriverWait(driver, 5)
usernameField = driver.find_element(By.ID, 'identifierId')
username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))
#username = webdriver.support.ui.WebDriverWait(driver, 5).until(when.element_to_be_clickable((By.ID, driver.find_element(By.ID, 'identifierId'))))
time.sleep(1)
username.send_keys("xxx)
Here I made an attempt of using the WebDriverWait function to allow time for the text box to be loaded before sending the keys.
But it errored and returned this:
File "/home/wit/PycharmProjects/gmeet-automation/main.py", line 20, in <module>
username = driver.find_element(By.ID, 'identifierId')
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/wit/PycharmProjects/gmeet-automation/venv/git/google-meet-project/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="identifierId"]"}
(Session info: chrome=96.0.4664.110)
The selector ID is correct, being identifierId, but since it isn't loaded properly the code could not find and input text. How should I correct the function used for waiting the element to load in order for this to work?

You are mixing selector with web element.
Here
usernameField = driver.find_element(By.ID, 'identifierId')
usernameField is web element. While here
username = wait.until(when.element_to_be_clickable((By.ID, usernameField)))
usernameField should be a string.
Also I'd suggest to prefer using visibility_of_element_located when possible instead of element_to_be_clickable
So, to make your code working try change it as following:
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.webdriver.common.by import By
import time; from datetime import datetime
driver = webdriver.Chrome('/home/wit/PycharmProjects/gmeet-automation/chromedriver')
wait = WebDriverWait(driver, 20)
driver.get("https://classroom.google.com")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="gfe-main-content"]/section[1]/div/div/div/ul/li[2]/a'))).click()
wait.until(EC.visibility_of_element_located((By.ID, "identifierId"))).send_keys("xxx)

Related

How to use the dropwon selenium selector?

I'm trying to use the selenium selector but I'm still getting this error message.
When I try 3Select(web.find_element("id",'ticket-quantity-selector-756706789'))", it doesn't seem to work on my terminal.
Thank you
self.execute(Command.GET, {'url': url})
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
-------------------------
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
web = webdriver.Chrome()
web.get('https://www.eventbrite.fr/e/masquerade-party-tickets-443305678217')
time.sleep(5)
cookie = web.find_element("xpath",'//*[#id="_evidon-accept-button"]')
cookie.click()
info = web.find_element("xpath",'//*[#id="eventbrite-widget-modal-trigger-443305678217"]')
info.click()
CoupC = Select(web.find_element("id",'ticket-quantity-selector-756706789'))
CoupC.select_by_visible_text("5")
//*[#id="ticket-quantity-selector-756706789"]
CoupS = Select(web.find_element("id",'ticket-quantity-selector-756706799'))
CoupS.select_by_visible_text("5")
Cliquer = web.find_element("xpath",'//*[#id="root"]/div/div/div/div/div[1]/div[1]/div/main/div/div[2]/div/div/div[2]/button')
Cliquer.click()
time.sleep(3)
Dropdowns are inside an iframe, so first you have to switch to iframe, then you have to select the option, try this, it's working:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element(By.ID, "eventbrite-widget-modal-443305678217")))
time.sleep(2)
driver.find_element(By.ID,'ticket-quantity-selector-756706789').click()
CoupC = Select(driver.find_element(By.ID,'ticket-quantity-selector-756706789'))
CoupC.select_by_visible_text("5")
CoupS = Select(driver.find_element(By.ID,'ticket-quantity-selector-756706799'))
CoupS.select_by_visible_text("5")
Cliquer = driver.find_element(By.XPATH,'//*[#id="root"]/div/div/div/div/div[1]/div[1]/div/main/div/div[2]/div/div/div[2]/button')
Cliquer.click()
If you need to switch to the main page from iframe, add this:
driver.switch_to.default_content()

Python > Getting stuck on Proceed to Checkout

New to Python.
Creating smoke test for Checkout process.
Am unable to click the Proceed to Checkout button.
On the Add-Ons page of the Checkout process, I am unable to click the Proceed to Checkout button which allows the user to go to the Attendee Info page of Checkout.
I've tried a few different things. Below is the most basic attempt.
Any guidance would be appreciated.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
username = 'bmendenhall#sans.org'
password = 'Lloydcole2!'
wait = WebDriverWait(driver, 10)
driver.get('https://www.sans.org/')
driver.find_element(By.CSS_SELECTOR, "a[aria-label='Log In']").click()
driver.find_element(By.CSS_SELECTOR, "input[id='username']").send_keys(username)
driver.find_element(By.CSS_SELECTOR, "input[id='password']").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "input[id='regularsubmit']").click()
time.sleep(3)
driver.get('https://registration.sans.org/sans/s/cart/0a62J000000ClOsQAK')
time.sleep(3)
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click()
time.sleep(10)
driver.close()
Error Messaging:
Traceback (most recent call last): File "/Users/BMendenhall/Desktop/Projects/sans-domain-testing/AddtoCartcontinue.py", line 28, in <module>
driver.find_element(By.CSS_SELECTOR, "button[title='proceed to checkout']").click() File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, { File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 435, in execute
self.error_handler.check_response(response) File "/Library/Python/3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[title='proceed to checkout']"} (Session
I don't know why but CSS_SELECTOR has problem even with button[title] but XPATH works for me
find_element(By.XPATH, "//button[#title='proceed to checkout']")
BTW:
first I tested it with find_elements (with s at the end) and it finds two buttons with 'proceed to checkout' - maybe this makes some problem with CSS.

How to perform Drag and Drop using Selenium and Python

Below I have given my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
browser.implicitly_wait(5)
print(browser.title)
source_element = browser.find_element(By.XPATH, "//*[#id='DHTMLgoodies_dragableElement5']")
target_element = browser.find_element(By.XPATH,"//*[#id='box106']")
actions = ActionChains(browser)
actions.drag_and_drop(source_element,target_element).perform()
My output:-
/home/halovivek/PycharmProjects/pythonProject/venv/bin/python /home/halovivek/PycharmProjects/pythonProject/draganddrop.py
Demo 2: Drag and drop
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/pythonProject/draganddrop.py", line 21, in <module>
actions.drag_and_drop(source_element,target_element).perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 75, in perform
self.w3c_actions.perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 77, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined
Stacktrace:
element.getInViewCentrePoint#chrome://remote/content/marionette/element.js:1185:5
getElementCenter#chrome://remote/content/marionette/action.js:1497:22
dispatchPointerMove/<#chrome://remote/content/marionette/action.js:1378:34
dispatchPointerMove#chrome://remote/content/marionette/action.js:1374:10
toEvents/<#chrome://remote/content/marionette/action.js:1145:16
action.dispatchTickActions#chrome://remote/content/marionette/action.js:1055:35
action.dispatch/chainEvents<#chrome://remote/content/marionette/action.js:1023:20
action.dispatch#chrome://remote/content/marionette/action.js:1029:5
performActions#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:459:18
receiveMessage#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:144:31
Process finished with exit code 1
I could not able to drag and drop it.
How to do the Perform action? I have tried many ways but I could not able to find a correct solution.
To drag_and_drop() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH:
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
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:
This works for me
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)
driver.quit()
Snapshot
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
'''drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)'''
#driver.quit()
This is the modified code.
Now its working. Thank you so much for the help.

selenium python wait time time out exception when it used to work before

I have code that is supposed to log into etsy after a certain amount of time. This code has not had any changes and used to work perfectly on an old machine that I had. Now however when I run this on another windows 10 machine, I get a time out exception.
Code:
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import datetime
import time
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
browser.find_element_by_id("sign-in").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
username = browser.find_element_by_id("username-existing")
password = browser.find_element_by_id("password-existing")
username.click()
username = browser.find_element_by_id("username-existing")
It gets to the wait.until line then it times out with this message:
> Navigating to website...
Traceback (most recent call last):
File "EstyBot.py", line 183, in <module>
main()
File "EstyBot.py", line 180, in main
get_item(item)
File "EstyBot.py", line 61, in get_item
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I have tried using sleep, but then I get the error that the element is not visible.
I have tried by XPATH and tried EC as element to be visible to only hit the same error message as below.
Seems the desired locators have changed and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sign-in"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input.input-large#join_neu_email_field"))).send_keys("Taryn#Antoinette.Raines")
browser.find_element_by_css_selector("input.input.input-large#join_neu_password_field").send_keys("Taryn_Antoinette_Raines")
Browser Snapshot:

Why selenium is unable to locate the Disqus comment section?

Hello
I created few selenium scripts before on python and they are working fine but don't know why it's not working on a website.
I'm trying to click on a element which appear when "DISQUS" is loaded on that website.
Disqus is a comment section on website which loads after website loading.
So I used sleep method but it didn't work so I tried it with python IDLE to execute code line one by one so I can run locate code once Disqus comment section is completely available but still getting same error. "Webdriver unable to locate element"
Here is my code.
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome('E:\\ChromeDriver\\chromedriver_win32\\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
dicuss = browser.find_element_by_class_name('textarea')
print(dicuss)
Error, I'm getting.
Traceback (most recent call last):
File "C:\Users\Acer-573G\PycharmProjects\Appium\venv\import selenium.py", line 12, in <module>
dicuss = browser.find_element_by_class_name('textarea')
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 563, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"textarea"}
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
I attached the screen shot of inspect element (class) from that website.
Inspect element screen shot
I'm new to programming, any help would be appreciated.
There are multiple iframes, so you have to switch to it before you want the interaction.
It's always a good practice if you switch the control of driver to default once you are done.
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
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")
wait = WebDriverWait(driver, 20)
visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))
action = ActionChains(driver)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))
driver.execute_script("window.scrollTo(0, 100)")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[#class='textarea']"))).send_keys("Hi")
driver.switch_to.default_content()
Hope this helps.
To send a character sequence to the Disqus comment section you need to:
Induce WebDriverWait for the desired parent frame to be available and switch to it
Induce WebDriverWait for the desired nested frame to be available and switch to it
Induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='disq2disqusCommentsHolder']")))
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id,'dsq-app') and #title='Disqus']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='textarea'][contains(#aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
Browser Snapshot:

Categories

Resources