I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Checkout" button using "element_to_be_clickable((By.XPATH".
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket
And this is my current code for the clicking:
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.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
checkout = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH("//img[contains(#src,'/static/images/checkoutv2.png.png')]"))).click()
I can see your xpath is not correct.
Your Xpath should be.
//img[contains(#src,'/static/images/checkoutv2.png')]
Your code should be.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
The link you provided contains hCaptcha, which is actually responsible to check whether you are a human being or a bot. I guess that it's also the reason, why you can't click any of the items on the page, because Selenium actually is nothing less than a bot.
You first have to pass the test by clicking on the images, which are asked for.
Related
Im trying to create automation for a cookie clicker website.
I need to click on elements (like the cursor element for example) on the website when they go from "blocked" to "unlocked" I have been trying for 2 days now and I have tried using the WebDriverWait but nothing is working no matter what my code does not detect when the element becomes available.
this is my code right now
import time
import ec as ec
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
Play = True
ser_obj = Service("\Progr\OneDrive\Documents\PythonFolder\chromedriver.exe")
driver = webdriver.Chrome(service=ser_obj)
driver.get(url="https://orteil.dashnet.org/cookieclicker/")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cc_btn.cc_btn_accept_all"))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.langSelectButton.title#langSelect-EN"))).click()
time.sleep(1)
Cookie = driver.find_element(By.CSS_SELECTOR, "#cookieAnchor #bigCookie")
while Play:
Cookie.click()
Cookie_number = (driver.find_element(By.XPATH,'//*[#id="cookies"]').text)
print(Cookie_number)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="product0"]'))).click()
and for whatever reason I cannot click on the cookie unless I have a time.sleep() method called and I do not know why. I have tried using WebDriverWait to wait when the cookie becomes avaible to click, but nope, it wont run without the time.sleep().
Any help would be great.
I have tried using if statments with the .isDisplayed() function.
I have tried using "try-except" methods.
I have tried giving Play a value and then saying when that value reaches 0, check to see if the cursor is available to click.
I have tried using CSS Selectors and Xpath
im quite noob in python and right now building up a web scraper in Selenium that would take all URL's for products in the clicked 'tab' on web page. But my code take the URL's from the first 'tab'. Code below. Thank you guys. Im starting to be kind of frustrated lol.
Screenshot
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
import time
from lxml import html
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
url = 'https://www.alza.sk/vypredaj-akcia-zlava/e0.htm'
driver.get(url)
driver.find_element_by_xpath('//*[#id="tabs"]/ul/li[2]').click()
links = []
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'blockFilter')))
link = driver.find_elements_by_xpath("//a[#class='name browsinglink impression-binded']")
for i in link:
links.append(i.get_attribute('href'))
finally:
driver.quit()
print(links)
To select current tab:
current_tab = driver.current_window_handle
To switch between tabs:
driver.switch_to_window(driver.window_handles[1])
driver.switch_to.window(driver.window_handles[-1])
Assuming you have the new tab url as TAB_URL, you should try:
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.key_down(Keys.CONTROL).click(TAB_URL).key_up(Keys.CONTROL).perform()
Also, apparently the li doesn't have a click event, are you sure this element you are getting '//*[#id="tabs"]/ul/li[2]' has the aria-selected property set to true or any of these classes: ui-tabs-active ui-state-active?
If not, you should call click on the a tag inside this li.
Then you should increase the timeout parameter of your WebDriverWait to guarantee that the div is loaded.
I'm working on a program which will trade stocks for me in the future. I have run into some problems after Logging In, typing the symbols of the stock, and now I need to click in some kind of way to be able to go on and buy the stock. I'm pretty confident that i know how to make a click with selenium but this tricks me out. I will give away the full code if anyone wants to try the program themself, just change the path to the browser! The account is fake, so don't worry.
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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
browser = webdriver.Chrome('/Users/larskvist/downloads/chromedriver')
browser.get('https://www.forex.com/en-uk/account-login/')
username_elem = browser.find_element_by_name('Username')
username_elem.send_keys('kebababdulaziz#gmail.com')
password_elem = browser.find_element_by_name('Password')
password_elem.send_keys('KEbababdulaziz')
password_elem.send_keys(Keys.ENTER)
time.sleep(5)
search_elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "input.market-search__search-input")))
search_elem.click()
search_elem.send_keys('FB')
time.sleep(2)
search_click_elem = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//app-market-table[#class='search-results-element ng-
star-inserted']//div[#class='price--buy clickable-price arrows-flashing']")))
search_click_elem.click
The IMG shows what i want to click, when clicked manually a pop up buy option appears.
Thanks in advance!
Seems like webdriver click is not working.
Induce JS executor to click on .
search_click_elem = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//app-market-table[#class='search-results-element ng-star-inserted']//div[#class='price--buy clickable-price arrows-flashing']")))
browser.execute_script("arguments[0].click();", search_click_elem)
Browser snapshot:
i am trying to using the selenium auto input the HTML code in http://ueditor.baidu.com/website/examples/completeDemo.html. My procedure is that click the html first, and then code HTML in, while the IDE always told me that cant locate the element. it mad me crazy that the after click the HTML Button, the element is right there, but always error on. i just wonder how can i write in box after click the HTML button by using selenium? thanks to the warm-hearted guy
import os,time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
chromePath = r'E:/Python/WEB/web-infor-transfer/monidenglu/chromedriver.exe'
wd = webdriver.Chrome()
loginUrl = 'http://ueditor.baidu.com/website/examples/completeDemo.html'
wd.get(loginUrl)
wd.find_element_by_xpath('//*[#id="edui4"]').click()
time.sleep(2)
wd.find_element_by_xpath('//*[#id="edui1_iframeholder"]/div/div[2]/div/div/div[2]/div/div[2]').send_keys('hello')
time.sleep(5)
wd.quit()
Try action chains. For example - sending keys to the browser itself worked:
wd.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div[2]/div/div/div[2]/div/div[2]/pre[2]/span').click()
actions = ActionChains(wd)
actions.send_keys('hello')
actions.perform()
I'm having trouble with the Selenium webdriver's click() feature in Python when I'm trying to click on buttons that only appear after you click on a parent button.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
driver = webdriver.Chrome()
elem0 = driver.find_element_by_id('export-icon-container') # this works
all_children_by_css = elem0.find_elements_by_css_selector("*") # this works, but doesn't click on the sub-button (XLS) one successfully when I run below...
all_children_by_css[0].click() # this just makes the parent button's little window appear and disappear, the same as elem0.click() does.
>>> all_children_by_css[0] # this is the webElement that I thought was for the XLS button
<selenium.webdriver.remote.webelement.WebElement (session="6b4a559408fa4d512f8596759d81eaf7",
element="d83be2ca-c879-4706-85ef-db7120d345a3")>
Basically, I want to export the XLS file via the Webdriver, so that later on I can do this on a loop with URLs of filters of the data.
I included annotated screenshots below, detailing the buttons I'm trying to click & the inspected code associated with them.
I would recommend to see if there an API on the page to export directly. if not, You would probably need dynamic wait. you can try something like below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
url = "https://law.lexmachina.com/cases/?pending-from=2000-01-01&pending-to=2000-02-01&filters=true&tab=summary&view=analytics&cols=475"
driver.get(url)
#Click on Export Icon
elem0 = driver.find_element_by_id('export-icon-container').click()
#Wait for XLS option to show up
wait = WebDriverWait(driver, 10)
ExportOption= wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#data-action='export-item'][contains(text(),'XLS')]")))
ExportOption.click()