I'm trying to log in to a webpage so that I can scrape data from it, but every time I try, i get the error Element Not Visible. The element I'm trying to click is a button inside of a modal window, but the html for that window only appears after a login button is clicked. I can click the login button, but I can't click the teacher button that pops up afterwards. Any ideas?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
login_url = 'https://edpuzzle.com/'
username = '***'
password = '***'
ChromeDriver = r'C:\Users\admin\Python\chromedriver.exe'
driver = webdriver.Chrome(ChromeDriver)
driver.get(login_url)
driver.find_element_by_css_selector('button.btn.btn-default-
transparent').click()
driver.find_element_by_css_selector('p.modal-title.text-lg.text-center.edp-
role-title')
driver.find_element_by_css_selector('button.btn.white-btn.btn-lg.btn-
block.big-btn.edp-teacher-role').click()
You're trying to click button before it actually visible. You need to wait some time:
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get(login_url)
driver.find_element_by_id('edp-login').click()
wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn.white-btn.btn-lg.btn-block.big-btn.edp-teacher-role"))).click()
Related
When the login button is clicked a popup window which shows login with google. I need to access into the sign in with google button of the popup. I've shown some previous example to handle the popup using driver.switch_to_window method and iframe method. But cant access the element in the popup window.
The Below code is showing noSuchElementException
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from time import sleep
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://shatkora.co/grocery")
main_window=None
while not main_window:
main_window=driver.current_window_handle
join_btn=driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[1]/div/header/div/button")
join_btn.click()
login_window=None;
while not login_window:
for handle in driver.window_handles:
if(handle!=main_window):
login_window=handle
break
# driver.switch_to(login_window)
driver.switch_to.window(login_window)
login_with_google_button=driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[6]/div/div/div/div[1]/button")
login_with_google_button.click()
You do not need to switch to any windows, also use absolute xpath with explicit waits.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get("https://shatkora.co/grocery")
wait = WebDriverWait(driver, 20)
join_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Join']")))
join_btn.click()
time.sleep(5)
button = driver.find_element_by_xpath("//h3[text()='Welcome to Shatkora!']/following-sibling::button")
driver.execute_script("arguments[0].click();", button)
after this code you will have to switch to new windows, which can be done via below code :
handles = driver.window_handles
driver.switch_to.window(handles[1])
I'm using Selenium in Python to log into a site and then set up certain values some of which are held in dropdown menus. In Chrome, when I inspect the dropdown menu, I get the following information:
The dropdown menu looks like this with these options:
There are multiple dropdown menus on this page. How would I change this specific dropdown menu's value when it seems that there is no unique ID for this element?
Here's the code I have so far for clicking on the login button, entering my credentials and logging in:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "/Users/mikefenty/Documents/chromedriver"
driver = webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 10)
driver.get("https://www.binarycent.com/")
link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Login")))
link.click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[ng-model='email']"))).send_keys(<login ID>)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[ng-model='password']"))).send_keys(<password>)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='ui primal']"))).click()
#EC.visibility_of_element_located(By.CSS_SELECTOR, "button[class='ui button green']"))
driver.quit()
I am using Selenium to remove a PopUp from investing.com but i am not able to recognise the PopUp correctly. The code I am using is ;
I need to click the button "Got it"...
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.common.exceptions import TimeoutException
from time import sleep
browser = webdriver.Chrome(executable_path="C:\\…….\chromedriver.exe")
browser.get("https://uk.investing.com/indices/mining-historical-data/")
browser.switchTo().frame(browser.findElement(By.xpath("//iframe[contains(#src,'https://prefmgr- cookie.truste-svc.net/cookie_js/cookie_iframe.html?parent=https://consent-pref.trustarc.com/?? layout=gdpr&type=investingiab2&site=investing-iab.com&action=notice&country=gb&locale=en&behavior=expressed&uid=dc7cd041-d8c4-4102-9522-1025da9b6e09&privacypolicylink=https://uk.investing.com/about-us/privacy-policy&iab=true&irm=undefined&from=https://consent.trustarc.com/')]"")));
sleep(3)
browser.findElement(By.xpath("//input[#name='Got it']")).click();
The popup is inside an iframe and elements ids change so it is a bit challenging.
Sometimes a second popup appears, make sure run this code before it.
This code is for closing the first popup.
from selenium import webdriver
from time import sleep
browser = webdriver.Chrome(executable_path="C:\\…….\chromedriver.exe")
browser.get("https://uk.investing.com/indices/mining-historical-data/")
sleep (3)
# get iframe element
title = "TrustArc Cookie Consent Manager"
frameElement = browser.find_element_by_xpath("""//*[#title="TrustArc Cookie Consent Manager"]""")
# get id iframe
id = frameElement.get_property("id")
print(id)
# switch to iframe element
browser.switch_to.frame(id)
sleep (1)
# get element to click
elemet = browser.find_element_by_xpath("""//*[#class='call']""")
print(elemet.get_attribute('innerHTML'))
# click element
elemet.click()
# switch the control back to the parent frame
browser.switch_to.default_content()
I've written a script that successfully makes the login on Instagram.
When I should go on my account, at home, the website displays a popup that asks you if you want notifications.
At this point, I tried a lot of solutions, but I got nothing.
I just want that, when the pop-up is displayed, the script should click on "Not now".
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
ids = []
driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
usm = driver.find_element_by_name('username').send_keys("**")
pwd = driver.find_element_by_name('password').send_keys("**")
btnLog = driver.find_element_by_tag_name('form').submit()
acpt = driver.find_element_by_xpath("//*[contains(#class, 'aOOlW HoLwm ')]")
In the image, there's the line of the button highlighted that I want to click:
Try the following code for this:
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
PS: I have used 10-second wait for the element to be clickable before click on it.
Hope it helps you!
To click() on the element with text as Not now on Instagram popup notification you can use the following solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
driver.find_element_by_name('username').send_keys("Giacomo")
driver.find_element_by_name('password').send_keys("Maraglino")
driver.find_element_by_tag_name('form').submit()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Non ora')]"))).click()
just add click to acpt, like this:
acpt = driver.find_element_by_xpath("//*[contains(#class, 'aOOlW HoLwm ')]")
acpt.click()
For some reason, the solution to this question didn't fully work for me. The script does click on "not now" then I'm redirected to the "Home" page only to find the 'activate notifications' pop-up waiting for me.
This is the solution that I came up with:
Go to https://instagram.com/
Wait for the page to load.
Find the "Not now" button on the button using the full XPATH.
Clicking on it.
Here's the code (inserted after identifying to my Instagram account):
driver.get("https://instagram.com/")
time.sleep(7)
acpt = browser.find_element(by=By.XPATH, value='/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div/div[3]/button[2]')
acpt.click()
So I have a python file that enters Instagram.com, puts the account credentials, and finally enters the keys into the search box after logging in.
Once it puts the keys into the search box, I can't see to submit the keys so Instagram can take me to the account page
(ex: I put #streetgoodies in the instagram search bar, i click enter, and it brings me to www.instagram.com/streetgoodies/)
Is there any way i can submit the keys into the search so it can redirect me to the search query that i have requested?
from time import sleep
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
# start a new browser session
browser = webdriver.Chrome('PATH/TO/DRIVER')
# navigate to a webpage
browser.get('https://www.instagram.com')
# find login link
login_elem = browser.find_element_by_xpath(
'//*[#id="react-root"]/section/main/article/div[2]/div[2]/p/a')
# click login in button
login_elem.click()
# send login info credentials to correct input boxes
browser.find_element_by_xpath("//input[#name='username']").send_keys('USERNAME')
browser.find_element_by_xpath("//input[#name='password']").send_keys('PASSWORD')
# click final login button
browser.find_element_by_xpath("//button[contains(.,'Log in')]").click()
# find hidden search bar
searchbox = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[#placeholder='Search']")
)
)
# send search into input
searchbox.send_keys('streetgoodies')
searchbox.submit()
The searchbox.submit() is causing the issue (I believe)
Thank you!!
I wrote down a script for you. Let me explain first:
I got direct login page. So, you do not need to search for login.
There should be WebDriverWait function because login page does not appear quickly.
Main problem of your code and instagram is there isn't any submit button. So there should be send_keys(Keys.ENTER)
One Keys.ENTER selects first item :) so I added another Keys.Enter
This code works:
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
# start a new browser session
browser = webdriver.Chrome('/pathtochromedriver')
# navigate to a webpage
browser.get('https://www.instagram.com/accounts/login/')
login_wait = WebDriverWait(browser, 10)
# click login in button
elem = login_wait.until(EC.visibility_of_element_located((By.XPATH, ".//input[#name='username']")))
elem.send_keys("usrname")
elem = login_wait.until(EC.visibility_of_element_located((By.XPATH, ".//input[#name='password']")))
elem.send_keys("passwd")
# click final login button
browser.find_element_by_xpath("//button[contains(.,'Log in')]").click()
# find hidden search bar
searchbox = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[#placeholder='Search']")
)
)
# send search into input
searchbox.send_keys('streetgoodies')
time.sleep(2)
# searchbox.submit()
searchbox.send_keys(Keys.ENTER)
time.sleep(1)
searchbox.send_keys(Keys.ENTER)