I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related
Hi i'm new at selenium and webscraping and i need some help.
i try to scrape one site and i need and i dont know how to get span class.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATCH = "/Users/bobo/Downloads/chromedriver"
driver = webdriver.Chrome(PATCH)
driver.get("https://neonet.pl")
print(driver.title)
search = driver.find_element_by_class_name("inputCss-input__label-263")
search.send_keys(Keys.RETURN)
time.sleep(5)
i try to extract this span
<span class="inputCss-input__label-263">Szukaj produktu</span>
I can see that you are trying to search something in the search bar.
First I recommend you to use the xpath instead of the class name, here is a simple technique to get the xpath of every element on a webpage:
right-click/ inspect element/ select the mouse in a box element on the upper left/ click on the element on the webpage/ it will directly show you the corresponding html/ then right click on the selected html/ copy options and then xpath.
Here is a code example that searches an element on the webpage, I also included the 'Webdriver-wait' option because sometimes the code runs to fast and can't find the next element so this function make the code wait till the element is visible:
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path="/Users/bobo/Downloads/chromedriver")
driver.get("https://neonet.pl") #loading page
wait = WebDriverWait(driver, 20) #defining webdriver wait
search_word = 'iphone\n' # \n is going to act as an enter key
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/main/div[1]/div[4]/div/div/div[2]/div/button[1]'))).click() #clicking on cookies popup
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/main/header/div[2]/button'))).click() #clicking on search button
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="root"]/aside[2]/section/form/label/input'))).send_keys(search_word) #searching on input button
print('done!')
sleep(10)
Hope this helped you!
wait=WebDriverWait(driver,10)
driver.get('https://neonet.pl')
elem=wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class,'inputCss-input')]"))).text
print(elem)
To output the value of the search bar you use .text on the Selenium Webelement.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm having a big problem with this one and I just don't know what to do now.
So, I'm trying to use selenium to get an information on a sort of a pop up.pop up
(This is this exact pop-up, it's on tiktok)
The HTML element of the button followers : Followers<
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
# Then, I want to click on "Followers" category but this is getting complicated here
# First idea to click on the category, check if it contains the word "followers"
if (driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').text) == "Followers" : # this line works, no problem
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this one doesn't work i don't know why
# So, second idea, try with the webdriverwait
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))) # this works
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this still doesn't work
# Third idea, instead o xpath, css-selector
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"span.jsx-2344352055:nth-child(5)"))) # work, no problem
driver.find_element_by_css_selector("span.jsx-2344352055:nth-child(5)").click() # doesn't work neither..
# Fourth and last idea, probably the least fine, get all the elements with the class name, and only one return Followers with the .text, but still the same problem
elements = (driver.find_elements_by_class_name("jsx-2344352055"))
for i in range(len(elements)) :
if elements[i].text == "Followers" :
elements[i].click() # but still doesn't work
Sometimes, it worked, like I don't know why or how but sometimes, the click works, but like 95% of the time, it doesn't and I really don't know why
Thanks in advance for your help !
You should wait for the button to be clickable using WebDriverWait and element_to_be_clickable expected condition. For example:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en")
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]')))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))).click()
Please try this one:
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS) ;
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
I hope this resolves your issue.
I've created a script in python using selenium to click on a like button available in a webpage. I've used xpath in order to locate that button and I think I've used it correctly. However, the script doesn't seem to find that button and as a results it throws TimeoutException error pointing at the very line containing the xpath.
As it is not possible to hit that like button without logging in, I expect the script to get the relevant html connected to that button so that I understand I could locate it correctly.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.instagram.com/p/CBi_eIuAwbG/"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,10)
driver.get(link)
item = wait.until(EC.visibility_of_element_located((By.XPATH,"//button[./svg[#aria-label='Like']]")))
print(item.get_attribute("innerHTML"))
How can I locate that like button visible as heart symbol using selenium?
To click on Like Button induce WebDriverWait() and wait for visibility_of_element_located() and below xpath.
Then scroll the element into view and click.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/p/CBi_eIuAwbG/")
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//button[.//*[name()='svg' and #aria-label='Like']]")))
element.location_once_scrolled_into_view
element.click()
You can do it like this
likeSVG = driver.find_element(By.CSS_SELECTOR, 'svg[aria-label="Like"]')
likeBtn = likeSVG.find_element(By.XPATH, './..')
likeBtn.click()
likeBtn is equal to the parent of the likeSVG div as you can use XPATH similar to file navigation commands in a CLI.
Try using the .find_element_by_xpath(xPath) method (Uses full xpath):
likeXPATH = "/html/body/div[1]/section/main/div/div[1]/article/div[2]/section[1]/span[1]/button"
likeElement = driver.find_element_by_xpath(likeXPATH)
likeElement.click()
I'm trying to create a macro that opens up all my online classes on Chrome (cause logging in is annoying, especially if you have to do it 8 times every morning).
BTW, I use Python 3.8, Chrome 81.0.4044.122, and the latest version of Selenium.
Until now, I clicked button using:
driver = webdriver.Chrome()
driver.find_element_by_xpath("PATH_OF_ELEMENT").click()
And then I find a login button that has a image instead of text.
I tried XPath, CSS Selector, id, name, the link of text, ActionChains (move_to) nothing works.
Here's the HTML:
here click me please.
The button I'm trying to press is the one with the tag a.
I spent 30 minutes googling about this and all I found was Stack Overflow questions from 6 years ago. They suggested I use WebDriverWait or change the frame. None of them worked (I might have made a mistake). I'm new to Selenium so please be kind and explain hard stuff.
How can I find the correct XPath of an image button and click it?
driver.find_element_by_css_selector('.nice-select').click()
driver.find_element_by_xpath("/html/body/div1/div[3]/div/div/div/div/div/div2/div1/div/ul/li2").click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div2/div/ul/li[19]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[3]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/span').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[4]/div/ul/li[3]').click()
driver.find_element_by_xpath('/html/body/div1/div[3]/div/div/div/div/div/div2/div[5]/a').click()
Try the following CSS Selector:
.my_menu>a
Code should look like:
driver.find_element_by_css_selector(".my_menu>a").click()
Also, try to locate the element with explicit wait:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".my_menu>a"))).click()
I have tested with the following code block (as result will be displayed popup with 2 options):
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://oc31.ebssw.kr/onlineClass/search/onlineClassSearchView.do?schulCcode=00898&schCssTyp=online_mid")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".my_menu>a"))).click()
time.sleep(5)
I hope it helps you!
When I run the code, the website loads up fine but then it won't click on the button- an error appears saying the element is not interacterble. What do I need to do to click the button? I am relatively new to this and would be grateful for any help.
I have already tried finding it by id and tag.
page = driver.get("https://kenpreston.co.uk/author/")
element = driver.find_element_by_id('mk-button-31')
element.click()
SOLVED:
I used driver.find_element_by_link_text and this worked fine.
I have checked the website and noticed that mk-button-31 is an id for a div tag and inside it there is an a tag. Try getting the url from the a tag and do another driver.get instead of clicking on it.
Also the whole div tag is not clickable so that is why you are getting this error.
Use sleep from time library to be sure page fully loaded
from time import sleep
page = driver.get("https://kenpreston.co.uk/author/")
sleep(2)
element = driver.find_element_by_id('mk-button-31')
element.click()
Looks like your element is not clickable you need to replace this id selector with the css and need to wait for the element before click on it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#mk-button-31 span"))
element.click();
Consider adding Explicit Wait to your script as it might be the case the DOM had finished loading and the button you're looking for is still not there.
The classes you're looking for are:
WebDriverWait
expected_conditions
Suggested code change:
#your other imports here
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
#your other code here
page = driver.get("https://kenpreston.co.uk/author/")
element = WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.ID, "mk-button-31")))
element.click()
More information: How to use Selenium to test web applications using AJAX technology