MysteryPlayer12
MysteryPlayer12

Reputation: 55

How do you find a Certain Text on a Website using Python Selenium?

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!

Upvotes: 0

Views: 297

Answers (1)

JeffC
JeffC

Reputation: 25596

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

Upvotes: 2

Related Questions