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()
Related
Need help refactoring this code using a function to reutilize WEbDriverWait and Search for IDs and Classes without repeating the same statements over and over. The script works as intended but I wanted to know a more "pythonic way" to code it. I've tried several ways but with no avail. "By." doesn't let me use a variable after itself to call for the Search Option nor concatenate a Str...I cant figure out how to do it.
def click_button(TypeSearch, nameElement):
newSearch = str(TypeSearch).upper()
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.newSearch, nameElement)))
driver.find_element(By.newSearch, nameElement).click()
click_button('ID', 'user-email')
click_button('ID', 'user-password')
Code Base:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# Credentials
username = "<SOME_USERNAME>"
password = "<SOME_PASS>"
# Initializing Chrome's webdriver
driver = webdriver.Chrome(PATH/TO/DRIVER, options = options)
# Loading atlassian's login page
driver.get("SOME_URL")
# Login Attempt || Username (Email Address)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'user-email')))
element.send_keys(username)
driver.find_element(By.ID,"login-button").click()
# Login Attempt || Password
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'user-password')))
element.send_keys(password)
driver.find_element(By.ID,"login-button").click()
# Click On Customer Service Desk Button
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'elrEjn')))
driver.find_element(By.CLASS_NAME, 'elrEjn').click()
# Click On Technical Support Option
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'joNJss')))
driver.find_element(By.CLASS_NAME, 'joNJss').click()
# Fill up Summary & Description Fields
dataSummary = "Summary Automation TEST RSM Tool"
dataDescription = "Descriptor Automation TEST RSM Tool"
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'summary')))
driver.find_element(By.ID, 'summary').send_keys(dataSummary)
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'ProseMirror')))
driver.find_element(By.CLASS_NAME, 'ProseMirror').send_keys(dataDescription)
# Locating the Submit Button
# Click() Function On Line 64 Commented For Testing Purposes
# NOTE 1: When Uploading into a Production Environment: Uncomment the Submit Click() Function
# NOTE 2: When Uploading into a Production Environment: Comment Line Above Submit Click() Function & Print Function # The Bottom
xpath_submit = '//*[#id="root"]/div[1]/div/div/div[2]/main/div/div[3]/form/div/div/div[6]/button[1]'
#submit = driver.find_element(By.XPATH, xpath_submit)
submit = driver.find_element(By.XPATH, xpath_submit).click()
#print(submit.text)
I've been trying to scrape this website Link I'm interested in clicking the first DONWLOAD button. However whenever I try to find any element that is a button, I can't find any.
Here is the code :
url = 'https://ember-climate.org/data/carbon-price-viewer/'
webdriver = create_driver()
with webdriver as driver:
driver.maximize_window()
driver.get(url)
wait = WebDriverWait(driver, 30)
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '//button')))
except:
pass
ids = driver.find_elements_by_xpath('//button')
for ii in ids:
print (ii.tag_name, ii.get_attribute('class'))
Is there anything wrong with the XPath or is it an issue with the website itself?
Your xpath is wrong, Download button is wrapped inside an span tag not button tag.
try this instead :
//span[contains(text(),'DOWNLOAD')]
also, I see it's in iframe, which can be located via
iframe[name='ETS']
CSS_SELECTOR, and we need to switch also to this iframe.
so in sequence the explanation would be :
You would have to click on cookies button.
Download button is in an iframe, we need to switch to iframe first and then we can interact with download button.
download button is a part of span tag not button tag.
Use Explicit waits.
Prefer id, css over xpath. (if they are unique in nature)
Launch browser in full screen mode.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 50)
driver.get("https://ember-climate.org/data/carbon-price-viewer/")
wait.until(EC.element_to_be_clickable((By.ID, "cn-accept-cookie"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='ETS']")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'DOWNLOAD')]"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
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 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"]')))
Here is a snippet of what I am trying to do, which is basically just adding an item to the cart and going to the checkout page using Selenium webdriver.
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
driver.find_element_by_xpath("//div[#id='exp-gridwall-wrapper']/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div[2]/p").click()
size_button = driver.find_element_by_css_selector(".exp-pdp-size-dropdown")
actions = ActionChains(driver)
actions.move_to_element(size_button).perform()
driver.find_element_by_id("buyingtools-add-to-cart-button").click()
checkout_button = driver.find_element_by_css_selector(".checkout_button")
actions = ActionChains(driver)
actions.move_to_element(checkout_button).perform()
I am currently getting this error during the step "click add to cart":
WebDriverException: Message: Element is not clickable
And I believe everything after that is broken as well...
The part that I'm stumbling on is identifying the correct css elements to click on.
If anyone can explain what I'm doing wrong or show me the correct element to choose so that I can add to cart and click checkout, it would be very much appreciated!
I'm pretty sure it's because you've opened the size dropdown and it covers the "Add to Cart" button.
Just maximize the browser window:
driver.maximize_window()
driver.get("http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
# ...
Plus, you have a typo in the button class name - it is checkout-button and not checkout_button. And, you need to add Explicit Waits to tackle the visibility and timing issues:
from selenium import webdriver
from selenium.webdriver import ActionChains
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://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120")
wait = WebDriverWait(driver, 10)
driver.find_element_by_xpath(
"//div[#id='exp-gridwall-wrapper']/div[2]/div[2]/div[2]/div/div/div/div/div/div[3]/div[2]/p").click()
# opening size dropdown
size_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".exp-pdp-size-and-quantity-container a.exp-pdp-size-dropdown")))
actions = ActionChains(driver)
actions.move_to_element(size_button).click().perform()
# selecting size
size = wait.until(EC.visibility_of_element_located((By.XPATH, "//li[contains(#class, 'nsg-form--drop-down--option') and normalize-space(.) = 'S']")))
actions = ActionChains(driver)
actions.move_to_element(size).click().perform()
# adding to cart
driver.find_element_by_id("buyingtools-add-to-cart-button").click()
# checkout
checkout_button = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".checkout-button")))
actions = ActionChains(driver)
actions.move_to_element(checkout_button).click().perform()
Works for me.