i want to log in to the Instagram page. I want to press the "Jetzt nicht" button, but my Selenium cant find it, no matter if im searching for the class name, or the tag name...
Could somebody please help me?
Btw: the code will be edited later :)
[see the class name etc]
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
#define webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.instagram.com/")
#wait 10s or till cookies loaded and accept
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "button")))
element.click()
#type username account, wait max 10s until searchbar loaded
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "username")))
element.send_keys("yeet")
#hit enter
element.send_keys(Keys.RETURN)
#type password
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "password")))
element.send_keys("yeet")
#hit enter
element.send_keys(Keys.RETURN)
#press the login button, press enter
element.send_keys(Keys.RETURN)
#click false on safe ur login information
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "B.sqdOP yWX7d y3zKF ")))
element.click()
sleep(30)
#quit
driver.quit()
driver.close()
(By.CLASS_NAME, "B.sqdOP yWX7d y3zKF ") locator is wrong!
By.CLASS_NAME locator receives single class name only, not multiple class names.
Also the long spaces between class names here absolutely not correct.
Also the class names you are using here seems to be dynamically changing, not something fixed.
And the B at the beginning looks to be absolutely irrelevant...
If you are using English version of instagram.com and wants to click on Not Now button which just appear once you login successfully. you can use the below code :
try:
# click false on save your login information
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Not Now']")))
element.click()
except:
print('something went wrong')
pass
should work for you.
Related
I am trying to input the name of the city that I want to depart from into Google Flights using python. After locating the element (the input box) in the html code I noticed that once you interact with the element it changes its class from class="II2One j0Ppje zmMKJ LbIaRd" to class="II2One j0Ppje zmMKJ LbIaRd VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe". Therefore driver.find_element(By.CSS_SELECTOR, '.LbIaRd').send_keys(city_name) only manages to send the first character of the string before the input box changes class. However I am unable to send any text to this element after it changes its class.
I have tried interacting with the element in order to change its class and then applying the same method to the new class of the input box:
x = driver.find_elements(By.CSS_SELECTOR, '.LbIaRd')[0]
x.click()
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, '.VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe').send_keys(city_name)
The code ends with no errors but it the text is not sent to the input box. There is only one element with .VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe locator.
I have also tried to click the element after the class change and then send the keys:
x = driver.find_elements(By.CSS_SELECTOR, '.LbIaRd')[0]
x.click()
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, '.VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe').click()
driver.find_element(By.CSS_SELECTOR, '.VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe').send_keys(city_name)
But I get the following exception:
selenium.common.exceptions.ElementClickInterceptedException:
Message: element click intercepted:
Element <input class="II2One j0Ppje zmMKJ LbIaRd VfPpkd-ksKsZd-mWPk3d-OWXEXe-AHe6Kc-XpnDCe, redacted html code">
is not clickable at point (612, 472).
Other element would receive the click: <input class="II2One j0Ppje zmMKJ LbIaRd", redacted html code>
Any help would be greatly appreciated.
Something like this? Try to send keys with ActionChains instead with send_keys function
# Needed libs
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.action_chains import ActionChains
import time
# We create the driver
driver = webdriver.Chrome()
# We instantiate the ActionChains which will allow to us to send several keys
action = ActionChains(driver)
# We maximize the window
driver.maximize_window()
# We navigate to the url
url='https://www.google.com/travel/flights'
driver.get(url)
departure = "Berlin, Germany"
destiny = "Austin, Texas, USA"
# We click on Reject cookies button from pop up
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//button)[1]"))).click()
# We clear the departure input in case there is something
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//input)[1]"))).clear()
# We click on it
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//input)[1]"))).click()
# We send the keys with the actionChains previously initialized
action.send_keys(departure).perform()
# We click on the first result
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, f"//ul[#role='listbox']//div[text()='{departure}']"))).click()
# Same for destiny input
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//input)[3]"))).click()
action.send_keys(destiny).perform()
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, f"//ul[#role='listbox']//div[text()='{destiny}']"))).click()
# Click on search button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, f"//button/span[text()='Search']"))).click()
My purpose was to write a program that could log in to Instagram and then like a picture using a certain link. And in the end, it should make a screenshot. The main trouble is that if the program gets a link for a second time my browser logs out of Instagram so I can't get a like on a photo. I've been searching for any decisions, but I found nothing about my problem. Please, help! Here's my code.
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.instagram.com')
def log_in(un, pw):
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
username.clear()
for i in un:
time.sleep(0.01)
username.send_keys(i)
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))
password.clear()
for i in pw:
time.sleep(0.01)
password.send_keys(i)
login_button = WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
driver.get('https://www.instagram.com/p/CSXFbIKjKxO/?utm_source=ig_web_copy_link')
driver.find_elements_by_link_text('Like').click()
filename = un + '.png'
driver.save_screenshot(filename)
log_in('login', 'password')
driver.close()
The like web element is basically a svg element, you could try the below xpath :
//*[name()='svg' and #aria-label='Like']
in code, replace this :
driver.find_elements_by_link_text('Like').click()
to
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and #aria-label='Like']"))).click()
Try this:
driver.get('https://www.instagram.com/p/CSXFbIKjKxO/?utm_source=ig_web_copy_link')
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".fr66n button"))).click()
I have a problem, I'm trying to make a script that automatically logs into this site https://www.nike.com.br/cosmic-unity-153-169-211-324680
The problem is that after a few seconds this page loads and you must select the size of the shoe and click on the button that is written "Faça login para comprar" ok, after my bot clicks there it opens a pop up where i must inform my email and password and click on the login button, the problem is that i'm trying and i can't click on the input to fill in the email and password and neither I can click on the login button, I believe that maybe it is due to the fact that it is inside a div
My code:
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
option = Options()
prefs = {'profile.default_content_setting_values': {'images': 2}}
option.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options = option)
driver.get("https//www.nike.com.br/cosmic-unity-153-169-211-324680")
wait = WebDriverWait(driver, 10)
wait.untilEC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//label[#for="tamanho__id40"]'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input#9f656f67-dbed-4cda-be83-0d0d0addc6f4'))).send_keys("test#gmail.com")
wait.untillEC.element_to_be_clickable((By.CSS_SELECTOR, 'input#7016e824-f431-43d0-b5c9-d0331c330014'))).send_keys("mypass")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#a7f38f9a-afd7-42ce-a978-314a7d484343'))).click()
Order this code and realize that it only works until you open the login popup, after that it generates this error:
selenium.common.exceptions.TimeoutException: Message:**
That pop up is inside an iframe, you need to switch your driver focus to that particular iframe.
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"nike-unite-oauth2-iframe")))
make sure once you are done with that pop up switch to default_content to proceed further.
driver.switch_to.default_content()
Update 1 :
driver = webdriver.Chrome("C:\\Users\\etc\\Desktop\\Selenium+Python\\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.nike.com.br/cosmic-unity-153-169-211-324680")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.cc-allow'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[#for='tamanho__id40']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#anchor-acessar-unite-oauth2'))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"nike-unite-oauth2-iframe")))
wait.until(EC.element_to_be_clickable((By.NAME, 'emailAddress'))).send_keys("test#gmail.com")
wait.until(EC.element_to_be_clickable((By.NAME, 'password'))).send_keys("mypass")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='ENTRAR']"))).click()
Consider this:
https://www.lanebryant.com/lace-trim-overpiece/prd-358988#color/0000006400
Click What's my size and then click get started. You'll see a drop down that has age ranges. I need to click those based on value that I have already. Consider my code:
wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div/div/div[2]/form/tfc-accordion-group/div[2]/div/div[2]/span/div[2]/div//span[text()=" + " "+ df_temp.age + " " + "]"))).click()
df_temp['age'] has value 16-18
But it doesn't click and gives timeout exception.
First click to the dropdown to open list of options, than click on item using //div[#value="ageRange.id" and contains(.,"16 - 18")] or //div[#value="ageRange.id" and normalize-space(.)="16 - 18"] xpath:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Age"]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[#value="ageRange.id" and contains(.,"16 - 18")]'))).click()
I hope this will help you, for the detail of every step see the code comments.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Configure the webdriver and webdriver-wait instances
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)
# Load the webpage
driver.get("https://www.lanebryant.com/lace-trim-overpiece/prd-358988#color/0000006400")
time.sleep(5)
# Open the popup
wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[#id="358988"]//td/a[#class="tfc-popup-click-open"]'))).click()
time.sleep(4)
# Find the popup iframe and make driver to focus the same
popup_iframe = wait.until(EC.visibility_of_element_located(
(By.XPATH, '//div[contains(#aria-label, "True Fit")]/iframe')))
driver.switch_to.frame(popup_iframe)
# To switch the focus back to the main content window,
# execute: driver.switch_to.default_content()
# Click the get-started button
wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[contains(#class, "tfc-cfg-nav-button")]/button'))).click()
# Find the age dropdown
age_dropdown = wait.until(EC.presence_of_element_located(
(By.XPATH, '//*[#class="tfc-select-custom"]/button')))
# Let's say you want to select age group:
# 35-44, so minAge here is 35
min_age = 35
# Activate the age dropdown and select and click the desired option
age_dropdown.click()
age_option = wait.until(
EC.visibility_of_element_located(
(By.XPATH, '//*[#class="tfc-select-custom"]//div[#class="tfc-select-body"]/div/span[contains(text(),%s)]' % (min_age))))
age_option.click()
time.sleep(20)
This question already has answers here:
Instagram search bar with selenium
(2 answers)
Closed 4 years ago.
Still working on this Instagram problem, i really need your help.
Here is my code:
input_button = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//button[#class ="chBAG"]')))
action=ActionChains(browser)
action.move_to_element(input_button)
action.click()
action.perform()
Here is the HTML:
<button class="chBAG">Fermer</button>
But I got a:
selenium.common.exceptions.TimeoutException: Message:
Could someone help me solve that problem ?
Thx
As per your requirement, you can use this code :
It'll search for "This is testing" string in search bar of Instagram.
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.get("https://www.instagram.com/accounts/login/")
username = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "username")))
password = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "password")))
username.send_keys("your username")
password.send_keys("your password")
driver.find_element_by_tag_name('button').click()
search = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[text()='Search']")))
search.click()
driver.find_element_by_xpath("//div[#role='dialog']/preceding-sibling::input").send_keys("This is testing")
You are getting this error because of generic class of input fields. Every time you open the page the classes names will be generated randomly. So how to fix it? For example like this:
imagine you want to log in, you have to:
click on email input field
type information
click on password input field
type information
click on Log in button
The sample code could be like this:
# xpath will work every time because it is static
email_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//form/div[1]/div/div/input'))) # locate email input
email_input =ActionChains(browser)
email_input.move_to_element(email_input)
email_input.click()
email_input.sendKeys("email")
email_input.perform()
password_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//form/div[2]/div/div/input'))) # locate password input
password_input =ActionChains(browser)
password_input.move_to_element(password_input)
password_input.click()
email_input.sendKeys("password")
password_input.perform()
login_button = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//span/button'))) # locate login button
login_button_action=ActionChains(browser)
login_button_action.move_to_element(login_button )
login_button_action.click()
login_button_action.perform()
To search something in search bar you have to do this:
click on search input field
type information
wait until results will load
click on the one of the results
Code:
import time # will be need below
search_input = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
"//input[#placeholder = 'Search']"))) # locate search input
search_input =ActionChains(browser)
search_input.move_to_element(search_input)
search_input.click()
search_input.sendKeys("fermer")
search_input.perform()
time.sleep(5) # wait 5 seconds until dropdown will appear
dropdown_menu = wait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
"//a[#href = '/explore/tags/fermermaid/']"))) # locate fermeraid
dropdown_menu = ActionChains(browser)
dropdown_menu.move_to_element(dropdown_menu)
dropdown_menu.click()
dropdown_menu.perform()