0

I am trying to make the code find a certain text on a website. This is probably very very basic, but I am very very new to this... So, I am wanting my code to read the website, and if it finds a certain text, which in my case is, "Currently unavailable.", it will continue the code and keep refreshing the page. Here is what my code is right now:

import time from selenium import webdriver # browser = webdriver.Chrome('D:\ChromeDriver\chromedriver.exe') # browser.get("https://www.amazon.com/TEAMGROUP-T-Force-Vulcan-3000MHz-Desktop/dp/B07QRSPFG7/") # import time time.sleep(10) # buyButton = False # while not buyButton: # try: addToCartBtn = addButton = (this is where I need to put the code to find the text, but I don't know how to) # print("Not in Stock") # time.sleep browser.refresh() 

Sorry, mind the #, that is just a space in lines in my code.

So, if I put a href or a find element in there, it works fine, but since I am looking for a text, it is not an element or a href. I do not know if I need to change the whole entire line of addToCartBtn = addButton = (blah blah blah)

If I need to change it, I am more than happy to do so.

Here is the text that I am wanting to put in for the addButton = (this is where I am putting in the text) https://i.sstatic.net/TYstF.png

(Sorry, I don't know how to add it as the whole code HTML, and I would rather not type out the whole thing, and I could not add a photo because I do not have 15 reputation, lol. Sorry!)

The "Currently unavailable." is what I am wanting the code to check. I want it to see if that text is on the page, and if so, continue the code, which I have tested and is working.

If anyone could help, that would be great!

Thank you in advance!

2
  • To add the HTML as text, open the dev console and right-click on the <div id="availability_feature_div" ...>, choose Edit as HTML, select all, and copy. Then put it through a beautfier and do some manual cleanup. Then you can come back and add it to your question as properly formatted code. Commented Apr 4, 2021 at 15:20
  • Also remove the #s on every other line since they aren't needed and don't contain any comment. Add the proper indent to your code, it matters in python. Commented Apr 4, 2021 at 15:22

1 Answer 1

2

You don't really need to find the "Currently unavailable" text. You can just check for the existence of the "Add to Cart" button.

browser = webdriver.Chrome('D:\ChromeDriver\chromedriver.exe') browser.get("https://www.amazon.com/TEAMGROUP-T-Force-Vulcan-3000MHz-Desktop/dp/B07QRSPFG7/") while len(browser.find_elements_by_id("add-to-cart-button")) == 0: print("Not in Stock") time.sleep(60) browser.refresh() # the product is in stock driver.find_element_by_id("buy-now-button").click() ...continue your script 

I wouldn't really suggest you do this because if the product is out of stock for days, surely you aren't going to let the script sit there and run for days? I would just have it check for the product once and quit. Then you can set the script to run once every 15 minutes or whatever timing you need. If you script sits there and hammers their server for an extended period of time, you're likely to get your account banned.

If you MUST find the "Currently unavailable" element, you can find it using this XPath

//div[@id='availability']/span[contains(text(), 'Currently unavailable')] ^ at any depth find a DIV ^ that has this ID ^ that has a child SPAN that contains this text 
Sign up to request clarification or add additional context in comments.

12 Comments

Dang man! Thank you so much! The only problem is with the first code, the "== 0", it is giving me the invalid syntax error... any idea why?
Also, I am getting this error with the 2nd code... imgur.com/a/IEf9bvZ
Here was the problem with the 1st one: imgur.com/a/3fjJC4e
The 2nd "code" is not python code, it's an XPath as I labelled it. I fixed the first one... it was just a missing ":" in the while.
Did you not find an example when you googled? There should be examples all over the internet. Why did you unaccept the answer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.