Recently i have began with Selenium in Python with Chromium Webdriver, but it doesn't fill in the Login Screen, what did i wrong? I think it has to be with the "find_element" function. Here's 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://instagram.com")
print(driver.title)
input_username = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")
input_username.send_keys("lowity")
time.sleep(5)
input_paswd = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")
input_paswd.send_keys("password")
time.sleep(5)
button_login = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button")
button_login.click()
time.sleep(5)
time.sleep(10)
driver.quit()
And the console has no error output.
You have to wait for element being clickable in order to click it. The best way is to user the WebdriverWait() function with until(EC.element_to_be_clickable) Here is the updated 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 = 'C:\\chromedriver.exe')
driver.get("https://instagram.com")
print(driver.title)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input"))).send_keys("lowity")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input"))).send_keys("janosch2005")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button"))).click()
driver.quit()
Related
I am going to scrap Glassdoor to extract companies' reviews! for the 1st step, I need to log in to extract all reviews, I put a time.sleep and wait time for clicking the "Singing" button, but I have still the following error:
raise TimeoutException(message, screen, stacktrace) TimeoutException
My code is like below:
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
from selenium.common.exceptions import TimeoutException, WebDriverException
from selenium.webdriver.chrome.options import Options
import time
import pandas as pd
from selenium.webdriver.common.action_chains import ActionChains
driver_path= r"C:\Users\TMaghsoudi\Desktop\chromedriver_win32.exe"
# chrome options
options = webdriver.ChromeOptions()
# options.add_argument("--start-maximized")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
Pros =[]
Cons=[]
Re_Titles =[]
Re_rates= []
Employ_status= []
Re_dates = []
# set driver
driver = webdriver.Chrome(driver_path, chrome_options=options)
# get url
url = "https://www.glassdoor.co.in/Job/index.htm"
driver.get(url)
time.sleep(3)
driver.find_element(By.CLASS_NAME, "HeaderStyles__signInButton").click()
time.sleep(5)
Enter_email= driver.find_element(By.ID, "modalUserEmail")
Enter_email.send_keys("XXXXX")
Enter_email.send_keys(Keys.ENTER)
time.sleep(3)
Enter_pass= driver.find_element(By.ID,"modalUserPassword")
Enter_pass.send_keys("XXXX")
time.sleep(3)
# SingIn= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[#class='d-flex align-items-center flex-column']/button[#class='gd-ui-button mt-std minWidthBtn css-1dqhu4c evpplnh0']")))
SingIn= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "evpplnh1")))
SingIn.click()
To send a character sequence to the Email and Password field you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:
Using CSS_SELECTOR:
driver.get("https://www.glassdoor.co.in/Job/index.htm")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.d-lg-block button.HeaderStyles__signInButton"))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#modalUserEmail"))).send_keys(email + Keys.RETURN)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#modalUserPassword"))).send_keys(password + Keys.RETURN)
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:
I am trying to automate some download of data and have an issue with accepting the cookies message.
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
from random import randint
import time
current_link = "https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F"
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(current_link)
#driver.switch_to.frame("cookieConsentIframe")
#driver.switch_to.frame(driver.find_element_by_name('cookieConsentIframe'))
try:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='cookieConsentIframe']")))
print(1)
driver.find_element(By.XPATH,'//button[#id="cookies-accept-all"]').click()
#driver.find_element(By.XPATH,'//button[text()="Accept"]').click()
except:
pass
time.sleep(randint(5,8))
driver.quit()
The code runs through (prints also the 1) but never clicks the button. Any suggestions? Tried so many things already.
You need to also wait out the button:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[#id="cookies-accept-all"]'))).click()
EDIT Here is a full example (selenium/chromedriver setup is for linux, but you need to observe only the imports, and part after defining the browser):
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'
browser.get(url)
WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='cookieConsentIframe']")))
## sortout cookie button
try:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//button[#id="cookies-accept-all"]'))).click()
print("accepted cookies")
except Exception as e:
print('no cookie button')
The element Accept 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.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'})
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.cookieConsent#cookieConsentIframe")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#cookies-accept-all span.btn-accept"))).click()
Using XPATH:
driver.execute("get", {'url': 'https://platform.smapone.com/Portal/Account/Login?ReturnUrl=%2FPortal%2F'})
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='cookieConsent' and #id='cookieConsentIframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='cookies-accept-all']//span[#class='btn-accept']"))).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
Browser Snapshot:
I am trying to open a page by inputting the stock code in the textbox and press search. But the input stock code automatically generates a list of other possible things to choose from. How can I choose the only first time of such list?
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
url = 'https://www1.hkexnews.hk/search/titlesearch.xhtml'
browser = webdriver.Firefox(executable_path = 'C:\Program Files\Mozilla Firefox\geckodriver.exe')
StockList = ['02192']
browser.get(url)
txtBox = browser.find_element_by_id('searchStockCode')
txtBox.send_keys(StockList[0])
txtBox.submit()
You can use below xpath to click on first option that is being populating.
//div[#class='slimScrollDiv']/descendant::tbody/tr[1]
and click it like this :
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='slimScrollDiv']/descendant::tbody/tr[1]"))).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
In case you are looking for full working 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
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox(executable_path = "C:\Program Files\Mozilla Firefox\geckodriver.exe")
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml")
wait = WebDriverWait(driver, 20)
StockList = ['02192']
wait.until(EC.element_to_be_clickable((By.ID, "searchStockCode"))).send_keys(StockList[0])
ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='slimScrollDiv']/descendant::tbody/tr[1]")))
ActionChains(driver).move_to_element(ele).click().perform()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[class*='applyFilters']"))).click()
from selenium import webdriver
userName = "myusername123"
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input').send_keys(userName)
Error
Unable to locate element: {"method":"xpath","selector":"//*. [#id="loginForm"]/div/div[1]/div/label/input"}
I'm trying to make a login in Instagram using selenium but I can't reach the username box with XPath
You could check with the explicitWait and use the name attribute
username=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[#name='username']")))
username.send_keys("userName")
password=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//input[#name='password']")))
password.send_keys("password")
import
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
See, there is name attribute available, you can try with that as below :-
driver.find_element_by_name('username').send_keys(userName)
name is always a reliable locator than xpath
if the above does not work, you can try with the Explicit waits as well :-
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
userName = "myusername123"
driver = webdriver.Chrome()
driver.get("https://www.instagram.com/")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, 'username'))).send_keys(userName)
wait.until(EC.element_to_be_clickable(( By.NAME, "password"))).send_keys('somepassword')
wait.until(EC.element_to_be_clickable(( By.XPATH, "//div[text()='Log In']/.."))).click()
I'm trying to connect to https://icloud.com using selenium, but I keep timing out while trying to find the email input, which has id='account_name_text_field'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox()
driver.get('https://icloud.com')
try:
email_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
(By.ID, 'account_name_text_field')))
except TimeoutException:
print('timed out at email_input')
driver.close()
email_input.send_keys('johndoe#icloud.com')
email_input.send_keys(Keys.ENTER)
password_input = driver.find_element_by_id('password_text_field')
password_input.send_keys('password')
password_input.send_keys(Keys.ENTER)
What am I missing here?
The email and password inputs are in an iframe, so I needed to switch the driver context to the iframe:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
def main(email, password):
driver = webdriver.Firefox()
driver.get('https://icloud.com/')
delay = 20
WebDriverWait(driver, delay).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'auth-frame')))
email_input = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'account_name_text_field')))
email_input.send_keys(email)
email_input.send_keys(Keys.ENTER)
password_input = WebDriverWait(driver, delay).until(EC.element_to_be_clickable((By.ID, 'password_text_field')))
password_input.send_keys(password)
password_input.send_keys(Keys.ENTER)
Now that you can sign in, glhf trying to get past 2FA...