I was trying to use python selenium to log in to a website. But the login is in a bootstrap modal dialog box, I cannot log in successfully using my code. Could someone please let me know what is wrong with my code?
I have tried the following code:
import requests
import time
import os, sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary(r'C:/Program Files/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko +'.exe')
driver.maximize_window()
wait = WebDriverWait(driver, 10)
time.sleep(1)
pageurl = "https://www.producemarketguide.com"
driver.get(pageurl)
popup = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="close-dialog"]')))
popup.click()
time.sleep(2)
login = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/header/div/div[2]/div[2]')))
login.click()
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
user.send_keys('xxx#gmail.com')
password = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[4]/input')))
password.send_keys('XXXX')
submit = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[6]/button')))
submit.click()
The error message I got is
Traceback (most recent call last):
File "pmg.py", line 34, in <module>
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
That modal dialog is an iframe, so you need to switch to iframe to enter the credentials, try the below code:
accept_cookies = driver.find_element(By.CSS_SELECTOR, "button#onetrust-accept-btn-handler")
if accept_cookies.is_displayed():
accept_cookies.click()
else:
print("Accept Cookies button not displayed")
popup_dialog = driver.find_element(By.ID,"popup-dialog-window")
close_icon = driver.find_element(By.ID, "close-dialog")
if popup_dialog.is_displayed():
close_icon.click()
else:
print("Welcome dialog not displayed")
time.sleep(1)
# Clicking Login/Register link
driver.find_element(By.CSS_SELECTOR, ".user-login.user-login-front.pull-right #user-piano-login-register").click()
time.sleep(1)
# Switch to the login dialog iframe
driver.switch_to.frame(driver.find_element(By.XPATH,".//iframe[starts-with(#id,'piano-id-')]"))
# enter the credentials and click Login button
driver.find_element(By.XPATH,".//input[#fieldloginemail]").send_keys("emailid")
time.sleep(0.5)
driver.find_element(By.XPATH,".//input[#fieldloginpassword]").send_keys("password")
time.sleep(0.5)
driver.find_element(By.XPATH,".//button[#actionlogin]").click()
Related
(updated question)
Hi.
I'm trying to click a button but it doesn't work.
I guess the error cause by Modal tags. But I'm not sure.
I got "NoSuchElementException" Error before I import "webdriver.support", Now I get "TimeoutException" error. I can't find a way. Can you help me?
ERROR
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Button
<button type="button" class="btn btn-warning btn-sm" id="btnReleaseVendorInfoModal"> 반출기본정보 수정</button>
Code
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# Login
# put id
driver = webdriver.Chrome()
driver.get("https://po-management.net/release/list")
time.sleep(1)
elem = driver.find_element_by_name("username")
elem.send_keys(usernameStr)
elem.send_keys(Keys.RETURN)
time.sleep(2)
# put password
password = driver.find_element_by_xpath('//*[#id="input73"]')
try:
ActionChains(driver).send_keys(Keys.TAB).send_keys(passwordStr).perform()
password.send_keys(passwordStr)
except StaleElementReferenceException:
pass
password.send_keys(Keys.RETURN)
# redirect
time.sleep(3)
url = "https://po-management.net/release/list"
driver.get(url)
# search
time.sleep(1)
search = driver.find_element_by_id("releaseSeqArray")
search.send_keys(vrorder)
search.send_keys(Keys.RETURN)
# click1
time.sleep(1)
driver.find_element_by_link_text(vrorder).click()
# click2
time.sleep(3)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-warning.btn-sm#btnReleaseVendorInfoModal"))).click()
You need to switch to the the frame by doing :
# after the frame is activated/displayed
modal = driver.find_element_by_id('releaseCauseInfo')
driver.switch_to_frame(modal)
# all the code that interacts with the modal can come here
NB: Don't forget to switch back to the default window after you're done with the modal by using driver.switch_to_default_content()
I've made a bot that automatically logs me to my Instagram account. Everything works fine, except the fact that I can't automatically press the "Log In" button. This is the error I get:
Traceback (most recent call last):
File "C:/Users/Carole/PycharmProjects/InstagramBot1/main.py", line 40, in <module>
LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER)
AttributeError: 'NoneType' object has no attribute 'send_keys'
Process finished with exit code 1
And here's my code:
# Importing
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support import ui
# Setting everything up
service = Service("\Program Files (x86)\chromedriver.exe")
# Maximizing Window
# Program Starts
RandomnessTime = input("Select Randomness time:")
InstagramUsername = input("Enter Username:")
InstagramPassword = input("Enter Password:")
#WaitRandomnessTime = "wait"+str(RandomnessTime)
driver = webdriver.Chrome(service=service)
driver.get('https://www.instagram.com/')
wait = ui.WebDriverWait(driver, 10)
driver.maximize_window()
wait.until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.bIiDR"))).click()
RandomnessTime = WebDriverWait(driver, (1 - int(RandomnessTime)))
# Logging in
# def Login(self, )
LoginUsername = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "username"))).send_keys(InstagramUsername)
LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER)
# Clicking "Not Now".
ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
Can someone spot the problem with what I've written?
I would rather have this xpath
//div[text()='Log In']//parent::button
to click on Login button.
In you code something like this :
ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.XPATH, "//div[text()='Log In']//parent::button"))).click()
The issue that you've with your code is :
ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword).send_keys(Keys.ENTER)
does not return anything, not sure why you have a returned variable for that.
If you wanna stick with your way, I would suggest to pass enter in same send_keys like this.
ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password"))).send_keys(InstagramPassword, Keys.ENTER)
send_keys() is a method and returns nothing, to approach what you want you need to:
LoginPassword = ui.WebDriverWait(driver, 10).until(ec.element_to_be_clickable((By.NAME, "password")))
LoginPassword.send_keys(InstagramPassword)
LoginPassword.send_keys(Keys.ENTER)
I need to click on two buttons located in the top command bar in a SharePoint list to complete a CSV download.
I have attempted using the 2 locators below and a explicit wait below with no success.
driver.find_element_by_xpath('//*[#id="appRoot"]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div/div/div/div/div/div/div[1]/div[4]/button/span').click()
driver.find_element_by_xpath('//*[#id="id__850-menu"]/div/ul/li[2]/button/div/span').click()
excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
Export button html:
<span class="ms-Button-flexContainer flexContainer-124" data-automationid="splitbuttonprimary"><i data-icon-name="svg/excel_16x1_5.svg" aria-hidden="true" class="ms-Button-icon icon-127"><img alt="" src="https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/brand-icons/product/svg/excel_16x1_5.svg"></i><span class="ms-Button-textContainer textContainer-119"><span class="ms-Button-label label-125" id="id__850">Export</span></span><i data-icon-name="ChevronDown" role="presentation" aria-hidden="true" class="ms-Icon root-33 css-81 ms-Button-menuIcon is-expanded menuIcon-187" style="font-family: FabricMDL2Icons;"></i><span class="ms-layer"></span></span>
CSV button html:
<div class="ms-ContextualMenu-linkContent linkContent-199"><span class="ms-ContextualMenu-itemText label-205">CSV</span></div
Error:
Traceback (most recent call last):
File "C:\Users\main.py", line 44, in <module>
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
File "C:\Users\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
My code:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pandas
from tkinter import messagebox
driver = webdriver.Chrome(r"C:\Users\chromedriver.exe") # change path
target_url = 'some_url'
driver.get(target_url)
email_field = (By.ID, "i0116")
password_field = (By.ID, "i0118")
next_btn = (By.ID, "idSIButton9")
two_factor_btn = (By.ID, 'idSubmit_SAOTCC_Continue')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(email_field)).send_keys("username")
# Click Next
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# wait for password field and enter password
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(password_field)).send_keys("password")
# Click Login
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# shows message box to promp input
messagebox.showinfo(title='2-step verification', message='Input on 2-step verification, (30 seconds timeout)')
time.sleep(30)
# 2 factor auth
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(two_factor_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# download csv
excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
# driver.find_element_by_xpath('//*[#id="id__124-menu"]/div/ul/li[1]/button/div')
# driver.find_element_by_xpath('//*[#id="ContextualMenu427"]/div/ul/li[2]/button/div/span')
Thanks for your help!
I don't find any such ID the HTML you have provided. Try with following xpath.
excel_btn = (By.XPATH, "//span[text()='Export']")
csv_btn = (By.XPATH, "//Span[text()='CSV']")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
Hope this will resolved your problem.
I'm working to automate web page where i'm unable to close the pop up. I have tried to refresh/switch to pop up window, nothing worked.
Code:
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='F:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.libertymutual.com/get-a-quote')
driver.maximize_window()
driver.find_element_by_xpath(
'//*[#id="1555468516747"]/section/div[2]/section/form/div[1]/div[3]/div/div[1]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[#id="zipcode-1555468516747-1555468516747"]').click()
driver.find_element_by_xpath('//*[#id="zipcode-1555468516747-1555468516747"]').send_keys('03878')
time.sleep(1)
driver.find_element_by_xpath(
'//*[#id="1555468516747"]/section/div[2]/section/form/div[2]/div[5]/div/div/button').submit()
time.sleep(5)
x=driver.find_element_by_xpath('//*[#id="discount-marketing-modal"]/header/button/svg').click()
driver.refresh()
If you want directly go to web page, https://buy.libertymutual.com/auto?city=Somersworth&jurisdiction=NH&lob=Auto&policyType=Auto&zipCode=03878
Replace last 3 lines of your code by below lines.Used action chain to click.
time.sleep(5)
ok = driver.find_element_by_xpath('//*[#id="discount-marketing-modal"]/footer/button')
ActionChains(driver).move_to_element(ok).pause(1).click(ok).perform()
x=driver.find_element_by_xpath('//*[#id="discount-marketing-modal"]/header/button/svg').click()
This is css selector for popup close button : .lm-Icon.lm-Icon-Close
But this element can't use the .click() method, the .click() method will produce this error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted:
Use ActionChains to solve this problem.
You can try the following code:
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 import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(executable_path='F:\chromedriver_win32\chromedriver.exe')
driver.get('https://www.libertymutual.com/get-a-quote')
driver.maximize_window()
wait = WebDriverWait(driver, 20)
auto_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='quotingText']//span[contains(text(), 'Auto')]")))
auto_element.click()
zip_code = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.zipcode")))
zip_code.click()
zip_code.send_keys("03878")
driver.find_element_by_css_selector('div.buttonWrapper button').submit()
popup_close_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".lm-Icon.lm-Icon-Close")))
action = ActionChains(driver)
action.move_to_element(popup_close_btn).click(popup_close_btn).perform()
WebDriverWait better than time.sleep(..)
hi there i need some help with the following code ....
i`m using selenium with python to send tweets automatically :
import time
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'C:/webDriver/chromedriver.exe')
driver.get("https://twitter.com/login")
username = driver.find_element_by_css_selector("input[placeholder='Phone, email or username']")
password= driver.find_element_by_css_selector("input[class='js-password-field']")
username.send_keys("xxxx")
password.send_keys("xxxx")
submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()
time.sleep(2.4)
autotw1 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '//*[#id="tweet-box-home-timeline"]/div'))).click
autotw1.send_keys("""testing """)
time.sleep(5)
tweet = driver.find_element_by_xpath('//span[#class="add-tweet-button "]//following-sibling::button[contains(#class,"tweet-action")]')
tweet.click()
But It gave me error :
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
i think the error came from xpath cause of the twitter website new style !!
You can use this locator:By.CLASS_NAME, 'DraftEditor-root'.
You must click on the element to bring up other elements to write the tweet, which is:By.CLASS_NAME, 'public-DraftEditorPlaceholder-root'), and use ActionChains to send text.
Following import:
from selenium.webdriver import ActionChains
Try the bellow code:
#submit login
submit = driver.find_element_by_xpath("//button[text()='Log in']")
submit.click()
autotw1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'DraftEditor-root')))
autotw1.click()
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CLASS_NAME, 'public-DraftEditorPlaceholder-root')))
ActionChains(driver).move_to_element(element).send_keys('testing').perform()
sendTw = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Tweet')))
sendTw.click()