May I know why it shows unable to locate element? - python

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

Related

Can't grap elements in selenium

I'm trying to create a Selenium script to export data from my homebanking account.
However, when I reach the login screen I'm facing some difficulties grapping the correct elements in order to input my credentials.
Please see the picture below:
This is my code so far:
I have commented the locations where I'm in trouble.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
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(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 20)
driver.get(str('https://www.lsb.dk/login'))
# close cookies popup
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'coi-banner__decline'))).click()
# click button to log in
buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Log på med MitID')]")
for btn in buttons:
try:
btn.click()
except:
pass
# From this point I have difficulties:
# Wait for the input field to load.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[#id='username7']")))
# Try to input my username (This doesn't work)
driver.find_elements((By.XPATH, "//*[#id='username7']")).send_keys('My usename')
# Try to locate the "Fortsæt" button
buttons1 = driver.find_elements(By.XPATH, "//*[contains(text(), 'Fortsæt')]")
# Click the "Fortsæt" button when found
for btn in buttons1:
try:
btn.click()
except:
pass

selenium python: element not intreractable

The problem is when I am clicking the first name field, the click is working, but when I am sending the text to the field, it is showing me the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Here is my code:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
s = Service("C:/Users/TUL/Desktop/cd/cd.exe")
driver = webdriver.Chrome(service=s)
driver.get('https://www.universityliving.com/')
driver.maximize_window()
time.sleep(3)
driver.find_element(By.XPATH, '/html/body/div[3]/div/div/div[2]/button').click()
time.sleep(1)
driver.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div[3]/div/div/a[1]/div/div/div').click()
time.sleep(2)
driver.switch_to.window(driver.window_handles[1])
driver.find_element(By.XPATH, "//div[#class='slick-slide slick-active slick-current']//div//img[#alt='Urbanest Carlton Melbourne']").click()
time.sleep(2)
driver.switch_to.window(driver.window_handles[2])
driver.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[3]/div[2]/div/div[2]/div/div[3]/div/div/div[3]/div/div[3]/div/ul/li[2]/p').click()
driver.current_window_handle
driver.find_element(By.XPATH, '//*[#id="enquiryForm"]/div[1]/div[1]/div/div/label').click()
driver.find_element(By.XPATH, '//*[#id="enquiryForm"]/div[1]/div[1]/div/div/label').send_keys('h')
time.sleep(3)
What I am expecting is, if the field is clickable then it must be text acceptable also.

How to scroll in Instagram following window using Selenium and Python?

How would I scroll in the following window on Instagram using Selenium and Python? I've tried everything I've found and none of them work.
Here's what I have to get me to the following window:
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("Enter Username Here")
password_input.send_keys("Enter Password Here")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/section/div/button').click()
sleep(2)
browser.find_element_by_css_selector('button.aOOlW:nth-child(2)').click()
browser.get('https://www.instagram.com/instagram/')
sleep(2)
browser.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
sleep(2)
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i=0
while(i<follower_number):
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])```
have a look at this Question. i tried that and worked.
my code
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
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.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("")
my_password=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("")
time.sleep(5)
login=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]')
login.click()
time.sleep(5)
driver.get("https://www.instagram.com/instagram/following/")
driver.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
time.sleep(5)
# this is not perfect but it will get your work done
# getting the number of followers
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i = 0
# looping for the exact number of followers ...
while(i<follower_number):
# as the website is dyanamic so updating the follwers list and also the webelement
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
# executing scroll into view script to view the element and thats gonna load the next element(follower ) ..ultimately your scrolling achived
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])
time.sleep(2)
print(i)
i=i+1

Can you help me type postcode into the postcode form box on this URL

I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to enter the postcode into the psotcode field/form box.=
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.currys.co.uk/app/checkout
And this is my current code:
# Selenium Tutorial #1
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.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
# Click "Accept All Cookies" or ignore if no pop up
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler")))
element.click()
except Exception:
pass
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "email-desktop")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")
driver.execute_script("arguments[0].click();", button)
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Continue to Basket"
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.sc-fzpjYC.gJohPa")
driver.execute_script("arguments[0].click();", button)
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Go to checkout"
button = driver.find_element_by_xpath("//button[contains(#data-component, 'Button')][contains(#type, 'button')]")
driver.execute_script("arguments[0].click();", button)
# Type in postcode
search=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[#contains='Postcode Checker")))
search.send_keys("NG229NU")
search.send_keys(Keys.RETURN)
your locator is wrong for search , use:
search = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//input[#aria-label="Postcode Checker"]')))

Can't exhaust the load more button to unveil all the headlines

I've tried to keep clicking on more button located at the bottom of a webpage (in it's landing page) to unveil all the headlines. The thing is when I execute my script, It only click once and then stop. How can I keep clicking on that button until there is no more option to click?
Link to that website
This is my script so far:
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
link = "https://www.newsnow.co.uk/h/Sport/Football/Championship/Transfer+News"
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(link)
while True:
try:
loadmore = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[class^='hl_more']")))
driver.execute_script("arguments[0].scrollIntoView();",loadmore)
loadmore.click()
except Exception: break
driver.quit()
Try below code to simulate required behavior:
header = driver.find_element_by_id("phead")
driver.execute_script('arguments[0].style.position = "absolute";', header)
while True:
try:
loadmore = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.hl_more.bg_tween:not(.nfloading)")))
driver.execute_script("arguments[0].scrollIntoView();",loadmore)
loadmore.click()
except: break

Categories