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()
Related
from selenium import webdriver
import time
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 random
import select
driver = webdriver.Chrome('ChromeDriver')
driver.get("https://devbusiness.tunai.io/login")
time.sleep(2)
driver.maximize_window()
#log in credentials
username = driver.find_element(By.NAME, "loginUsername");
username.send_keys("xxxxx");
password = driver.find_element(By.NAME, "loginPassword");
password.send_keys("xxxxx");
login = driver.find_element(By.XPATH,"//*[#id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
login.submit();
time.sleep(3)
driver.get("https://devbusiness.tunai.io/dashboard/my_salon_user")
time.sleep(3)
randomUsername = random.choice(["dayon.salon3#tunai","dayonmanager#tunai","Dayon.der#tunai"])
driver.find_element(By.XPATH, "//tbody[#role='rowgroup']/tr[#role='row']/td/a[text()='"+ randomUsername +"']").click()
print("Username selected: ", randomUsername)
time.sleep(5)
driver.find_element(By.XPATH,"//*[#id='page-content']/div/div[3]/div/div[2]/div/div/div[2]/div/div[1]/header/a").click()
time.sleep(5)
# Get the list of elements
elements = driver.find_elements(By.CLASS_NAME,'custom-control-input')
# Select a random element from the list
random_element = random.choice(elements)
driver.execute_script("arguments[0].click();", random_element)
# Click on the selected element
random_element.click()
print("Element selected: ", random_element)
time.sleep(5)
driver.find_element(By.XPATH,"//*[#id='accKey']").click()
time.sleep(5)
I've been add "argument.click[]","webdriver wait until EC to be clickable" but still showing "Element not interactable. What would be the other possible solution? Hope someone could clarify for me. Thanks and have a nice day.
The problem is that you have picked an option randomly from the list but haven't used it correctly. The usage random.choice(element) is incorrect.
You can use the below code as a reference for the problem that you are facing. Here //tbody[#role='rowgroup']/tr[#role='row']/td[2]/a is thy xpath for the table cell that contains all username fields (plus some other elements)
randomUsername = random.choice(["dayon#tunai","dayon.salon3#tunai","dayonmanager#tunai"])
driver.find_element(By.XPATH, "//tbody[#role='rowgroup']/tr[#role='row']/td/a[text()='"+randonUsername+"']").click()
in this first ine, this will first create a variable with a random value from your list.
Then it will pass the variable with the randomized option in the xpath for the table cell and click it. A new dialog with "Blocked Permissions" will open
To click on specific username listed on table you need to use unique xpath to click.
After login added this code.
driver.get("https://devbusiness.tunai.io/dashboard/my_salon_user")
time.sleep(3)
randomtext = random.choice(["dayon#tunai","dayon.salon3#tunai","dayonmanager#tunai"])
element = driver.find_element(By.XPATH,"//table[#id='__BVID__74']/tbody//tr//td[2]//a[text()='{}']".format(randomtext))
element.click()
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.
This question already has answers here:
Unable to type within username field within ProtonMail signup page using Selenium and Python
(2 answers)
Closed 2 years ago.
driver.find_element_by_id('username').send_keys('thisIsAString')
Selenium is able to type in the password fields but not the username field. I'm using the same code for both of them but for some reason the username is acting weird.
<input placeholder="Choose username" required="" name="username" messages="[object Object]" iframename="top" pattern=".{1,40}" id="username" class="input">
That is the username field's HTML
Any help will be greatly appreciated!
Edit:Code:
from selenium import webdriver
import time
url = 'https://protonmail.com/signup'
driver = webdriver.Chrome('chromedriver')
driver.get(url)
time.sleep(2)
driver.find_element_by_class_name('panel-heading').click()
time.sleep(4)
driver.find_element_by_id('freePlan').click()
time.sleep(20)
driver.find_element_by_id('username').send_keys('thisIsAString')
time.sleep(1.5)
driver.find_element_by_id('password').send_keys('passwordForUser')
time.sleep(2)
driver.find_element_by_id('passwordc').send_keys('passwordForUser')
time.sleep(2)
driver.find_element_by_class_name('signUpProcess-btn-create').click()
time.sleep(1)
driver.find_element_by_id('confirmModalBtn').click()
Error Message:selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"username"} (at line 21)
I can see there are couple of iFrames in your page. Also since its an Ajax page lot of components are loading separately so best bet here is explicitwait.Note, time.sleep() is not a very realiable method of wait and it will make your test unpredictable. Use below code:
driver.get("https://protonmail.com/signup")
#Wait for upto 30 sec for Free plan div to appear
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[#aria-controls='plan-free']"))).click()
#Wait for upto 30 sec for Select Free plan button to appear
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, "freePlan"))).click()
#Wait and switch to iframe containing user id field
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#title ='Registration form' and #class='top']")))
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, "username")))
driver.find_element_by_id('username').send_keys("abc")
driver.switch_to.default_content() # Switch to default window
driver.find_element_by_id('password').send_keys("abc")
driver.find_element_by_id('passwordc').send_keys("abc")
Note, if you wish to enter recovery email and click on Create Account button there is one more iframe which you need to switch. Use below:
#To enter recovery Email and click on create account button
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[#title ='Registration form' and #class='bottom']")))
driver.find_element_by_id('notificationEmail').send_keys("xyz")
driver.find_element_by_name('submitBtn').click()
driver.switch_to.default_content()
Your input field is not loeaded yet, but the code is accessing it. You can use the following code to wait until your input field is loaded. And then you can access it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "username"))
)
finally:
driver.quit()
Generally always make sure your page is fully loaded before accessing any elements on it.
UPDATE:
I just saw that you are using an iFrame here. Try adding an ID to the iFrame tag and then wait for it to load like this:
HTML:
<iframe id="iFrameTagID" title="Registration form" scrolling="no" class="top" data-name="top" sandbox="allow-scripts allow-same-origin allow-popups allow-top-navigation" src="https://secure.protonmail.com/abuse.iframe.html?name=top"></iframe>
Selenium:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "iFrameTagID"))
)
finally:
driver.quit()
Hi I am using Selenium to visit a instagram profile and click on the first post. Language I am using is python. As an example, I am trying to open the profile instagram.com/yotta_life and I got the XPATH of first post : //*[#id=\"react-root\"]/section/main/article/div/div[1]/div[1]/div[1]/a/div/div[2]
I can visit the profile in Selenium but error message is showing, Element is not clickable at point (1015, 635)
This is my code :-
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
import time
def login(driver):
username = "username" # <username here>
password = "password" # <password here>
# Load page
driver.get("https://www.instagram.com/accounts/login/")
# Login
driver.find_element_by_xpath("//div/input[#name='username']").send_keys(username)
driver.find_element_by_xpath("//div/input[#name='password']").send_keys(password)
driver.find_element_by_xpath("//span/button").click()
widgets_click = WebDriverWait(driver, 500000).until(
EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div/div[2]/div/div/button"))
)
widgets_click.click()
def scrape(driver, account):
# Load account page
driver.get("https://www.instagram.com/{0}/".format(account))
# This is the first post click which is not working
postdivrow_click = WebDriverWait(driver, 500000).until(
EC.element_to_be_clickable((By.XPATH, "//*[#id=\"react-root\"]/section/main/article/div/div[1]/div[1]/div[1]/a/div/div[2]"))
)
postdivrow_click.click()
if __name__ == "__main__":
chromedriver = "Documents/chromedriver"
driver = webdriver.Chrome(chromedriver)
try:
login(driver)
followers = scrape(driver, "yotta_life")
finally:
driver.quit()
Problem here is that element is not clickable. I think, I am not using correct XPATH. What is the recommended way to do it.
Here, I have to use
driver.execute_script("window.scrollTo(0, Y)")
Y is the height, to scroll down the page.
I am getting very frustrated trying to login to costar.com with python and selenium. I have tried it on the chrome browser and firefox browser, but can't figure out the correct code.
I have logged into other websites, but cannot figure out how to input text into the login boxes for this site.
Here's what I have so far:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome(executable_path="C:/Users/Gus Gabel/Anaconda/chromedriver.exe")
browser.get("http://www.costar.com")
browser.maximize_window()
#the username and password boxes are hidden until you press the login button on the home page
login = browser.find_element_by_id("loginLink")
login.click()
#now that the username and password boxes are available, I've tried finding the elements by class_name, xpath, (BY.NAME), id, etc and nothing has worked
#here are a few codes that haven't worked and the errors associated
user = browser.find_element_by_id('username')
NoSuchElementException: Message: no such element
user = browser.find_element_by_class_name('usernameNew')
NoSuchElementException: Message: no such element
#when i try to use the above code with "elements" instead of "element", no error message pops up.
user = browser.find_elements_by_class_name('usernameNew')
#but then whey i try to choose which element by doing this
user = browser.find_element_by_class_name('usernameNew')[0]
NoSuchElementException: Message: no such element
How is it possible that there can be a list of elements, but yet not have an initial element?
If anyone can figure out how to input text into the username and password text boxes of costar.com, I will be greatly appreciative. I can't figure this out for the life of me!
To enter text into an input box with Selenium (e.g, your user name into the username field), use the send_keys method of the element:
input_element.send_keys('some_text_string')
You can also try Selenium's action_chains module, as in the following code (untested), to get past the roadblock from #dm295's answer:
from selenium import webdriver
from selenium.webdriver.common import action_chains
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")
action = action_chains.ActionChains(driver)
login = driver.find_element_by_class_name("login-icon")
login.click()
driver.switch_to.frame(driver.find_element_by_id('custlogin'))
username = driver.find_element_by_id('username')
action.move_to_element(username).perform()
username.send_keys('Gus Gabel')
A couple of things:
(a) you have to wait until the page is loaded (or at least the part of the page that you are interested in)
(b) only maximized browser window works for me (depends on device/resolution)
(c) you are trying to click the wrong element
import time
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
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")
try:
# wait 15 seconds till the login link is present
login = WebDriverWait(driver, 15).until(
EC.presence_of_element_located((By.CLASS_NAME, "login-icon"))
)
login.click()
# just sleep 5 seconds to show that the login link was clicked
time.sleep(5)
# do other stuff (probably fill in username and password) ...
finally:
driver.quit()
Read the documentation regards waiting in selenium.
EDIT
Manged to get one step further (switch to iframe), but the following code raises
selenium.common.exceptions.ElementNotVisibleException: Message:
Element is not currently visible and so may not be interacted with
from selenium import webdriver
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("http://www.costar.com")
try:
login = driver.find_element_by_class_name("login-icon")
login.click()
# switch to the custlogin iframe
driver.switch_to.frame(driver.find_element_by_id('custlogin'))
username = driver.find_element_by_id('username')
# this will raise:
# selenium.common.exceptions.ElementNotVisibleException: Message:
# Element is not currently visible and so may not be interacted with
username.send_keys('username')
finally:
driver.quit()