Home > database >  How can I detect if a word is present on a Chrome page using selenium (Python)?
How can I detect if a word is present on a Chrome page using selenium (Python)?

Time:12-29

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

CodePudding user response:

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

CodePudding user response:

Try this out:

if(driver.getPageSource().contains("Inventory"))
{
   
}

else
{
    driver = webdriver.Chrome()
    driver.get(response.url)   
    time.sleep(3)   
    driver.refresh()
}

CodePudding user response:

With SeleniumBase, you can use the self.assert_text(test) or self.assert_text(text, selector) methods to assert text on a page (or in an element if the second optional selector argument is provided). You can also use the methods: self.is_text_visible(text) and self.is_text_visible(text, selector), which return True or False.

from seleniumbase import BaseCase

class DemoSiteTests(BaseCase):
    def test_demo_site(self):
        self.open("https://seleniumbase.io/demo_page")
        self.assert_text("Demo Page")
        self.assert_text("Demo Page", "h1")

Here's a longer example that uses those: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py

  • Related