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()
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
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("kevin#tunai");
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/salon_report_voucher")
time.sleep(3)
driver.find_element(By.XPATH, '//*[#id="packageTable"]/tbody[1]/tr/td[1]').click()
time.sleep(2)
driver.find_element(By.XPATH,'//*[#id="packageTable"]/tbody[1]/tr').click()
time.sleep(2)
#trigger the element before click
voucher = driver.find_element(By.XPATH, "//*[#id='packageTable']/tbody[2]/tr[1]")
driver.execute_script("arguments[0].click();", voucher)
time.sleep(2)
driver.find_element(By.XPATH, '//*[#id="packageTable"]/tbody[2]/tr[1]')
time.sleep(2)
#trigger the detail button
detail = driver.find_element(By.XPATH, "//*[#id='checkAll']")
driver.execute_script("arguments[0].click();", detail)
time.sleep(5)
driver.find_element(By.XPATH, "//*[#id='checkAll']")
time.sleep(5)
driver.find_element(By.XPATH, '//*[#id="voucherModal___BV_modal_body_"]/div/div/button[1]').click()
time.sleep(5)
I wanna click on the edit new price and enter a new price, save it and loop it. For now, I am not allow to enter the new price and proceed. I copied the Xpath and tried, but it failed so i m thinking because of the popout page. thats why it failed.
To click on Edit Price button. use webdriverwait() and wait for element to be clickable and following xpath.
Then wait for model to pop up and then enter the value.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='voucherModal___BV_modal_content_']//button[text()='Edit Price']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='voucherEditModal___BV_modal_content_']//input[#data-maska-tokens]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#id='voucherEditModal___BV_modal_content_']//input[#data-maska-tokens]"))).send_keys("1000")
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
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.
I am trying to click on the first result on this page, but all the options I tried didn't work.
Firstly I just login into the website with email: kocianlukyluk#gmail.com and password: Redfinpython06. Here is the code for it:
driver = webdriver.Chrome("C:\\Users\\kocia\\OneDrive\\Plocha\\Python\\nastaveni\\chromedriver.exe")
driver.get('https://www.redfin.com/myredfin/favorites')
email = 'kocianlukyluk#gmail.com'
password = 'Redfinpython06'
time.sleep(3)
driver.find_element_by_xpath(
'//*[#id="content"]/div[6]/div/div[2]/div/div/form/span[1]/span/div/input').send_keys(email)
time.sleep(3)
driver.find_element_by_xpath(
'//*[#id="content"]/div[6]/div/div[2]/div/div/form/span[2]/span/div/input').send_keys(password)
time.sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()
But the problem is after login i can't click on the first result on the page.
Here is what i tried:
result = driver.find_elements_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.find_element_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]").click()
or
result = driver.find_elements_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.click()
or
result = driver.find_element_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]")
result.click()
Thank you so much for help.
I hope that is a dummy email and password that you are just using for testing purposes :)
Below clicks on the first house picture in the list. I also cleaned up your email and password xpath designations. You can see how much easier it is to grab them by name
Also, you may want to put proper wait methods around these find elements. Using sleep generally is not recommended
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 time import sleep
driver = webdriver.Chrome()
driver.get('https://www.redfin.com/myredfin/favorites')
email = 'kocianlukyluk#gmail.com'
password = 'Redfinpython06'
sleep(3)
driver.find_element_by_name(
'emailInput').send_keys(email)
sleep(3)
driver.find_element_by_name(
'passwordInput').send_keys(password)
sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()
sleep(3)
first_house = driver.find_element_by_xpath("//div[#class='FavoritesHome'][1]//img")
first_house.click()
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()
Code at Selenium by python
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://motul.lubricantadvisor.com/Default.aspx?data=1&lang=ENG&lang=eng")
def getallcars():
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage")))
driver.find_element(By.ID, "ctl00_ContentPlaceHolder1_rptCategoryBtn_ctl01_btnImage").click()
wait.until(EC.presence_of_element_located((By.ID, "ctl00_ContentPlaceHolder1_lblSelectedMake")))
driver.find_element(By.ID, 'ctl00_ContentPlaceHolder1_lblSelectedMake').click()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ctl00_ContentPlaceHolder1_lstMake")))
el = driver.find_element(By.NAME,"ctl00$ContentPlaceHolder1$lstMake")
car =[]
for option in el.find_elements(By.TAG_NAME,'option'):
car.append((option.text).encode('utf8'))
return car
cars=getallcars()
for value in cars:
drop = driver.find_element(By.CSS_SELECTOR, '#ctl00_ContentPlaceHolder1_lstMake')
sel = Select(drop)
sel.select_by_visible_text(value)
time.sleep(2) #<---- THIS POINT!!
driver.find_element(By.ID,'ctl00_ContentPlaceHolder1_HeaderModel').click()
el2 = driver.find_element(By.NAME, "ctl00$ContentPlaceHolder1$lstModel")
print "The models for %s are:" %value
for option in el2.find_elements(By.TAG_NAME,'option'):
print option.text
action = ActionChains(driver)
action.move_to_element_with_offset(el2, 300, 200)
action.click()
action.perform()
driver.find_element(By.CSS_SELECTOR,'#ctl00_ContentPlaceHolder1_HeaderMake').click()
I have been make the crawler. I don't understand completely yet. so I have a question. maybe It's 34line at code. I was mark about #
it's use be the "time.sleep(2)" method. because It didn't detect the select box when It's change about "sel.select_by_visible_text(value)"
how can I do that? I don't want to use the "time.sleep(2)"method.
already I tried "expected_conditions.presence_of_element_located" It doesn't work. I guess It's problem about dropbox. this size is not basically because It did well when I tried another size tried "expected_conditions.presence_of_element_located"
Explicit wait will not work, because the conditions you can use are "element to be clickable", "element to be visible" and like that.
The element that you using for explicit wait is available and clickable also, but its failing because other element is overlapping it.
Since the other element's overlap takes time to disappear, we have to wait for the overlap to disappear before we can click on the element.
Explicit wait can wait for element to appear and clickable, which it is already is but it is being hidden by other element.
In this scenario, we have to use time.sleep() to put a hard wait