This is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import random
import time
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
driver.get("https://www.ti.com/store/ti/en/p/product/?p=CD4040BE&keyMatch=CD4040BE")
time.sleep(1)
accept_and_proceed = driver.find_element_by_xpath('//*[@id="consent_prompt_submit"]')
accept_and_proceed.click()
region = Select(driver.find_element_by_xpath('//*[@id="llc-cartpage-ship-to-country"]'))
region.select_by_visible_text('Italy')
continue1 = driver.find_element_by_xpath('//*[@id="llc-cartpage-ship-to-continue"]')
continue1.click()
On the website there is the word Inventory. How can I detect if the word Inventory is present on the page? If it is present then print x and if it is not refresh the page until it will be
To validate if the word Inventory is present on the page you can use a try-except{}
block inducing WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategies:
Code Block:
driver.get("https://www.ti.com/store/ti/en/p/product/?p=CD4040BE&keyMatch=CD4040BE")
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='llc-cartpage-ship-to-country']")))).select_by_visible_text('Italy')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='llc-cartpage-ship-to-continue']"))).click()
while True:
try:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'u-tablet-up-only')]//span[@class='product-note product-availability' and contains(., 'Inventory')]")))
print("x")
break
except TimeoutException:
driver.refresh()
continue
driver.quit()
Console Output:
x
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException