I am trying to wait for certain text to show up in the web page after I click on the 'Next' button.
if pageno == 1, the code works as expected.
when pageno != 1, I don't know how to wait for the <li tag's value to be equal to 'expected_text' which assures that
the page I am loading is fully loaded before continuing.
The page does indeed actually get loaded, but since I am only waiting for the ".pageinfo" css selector, which is already there from the previous page,
code continues too soon. How do I get it to wait for the text of <li tag to be equal to 'expected_text'?
code:
if pageno == 1, the code works as expected.
when pageno != 1, I don't know how to wait for the <li tag's value to be equal to 'expected_text' which assures that
the page I am loading is fully loaded before continuing.
The page does indeed actually get loaded, but since I am only waiting for the ".pageinfo" css selector, which is already there from the previous page,
code continues too soon. How do I get it to wait for the text of <li tag to be equal to 'expected_text'?
code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from pathlib import Path
import os
import sys
class ByCSS_SELECTOR:
def __init__(self) -> None:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
self.HomePath = Path(".")
self.NewHampshireBusinessListing = 'https://quickstart.sos.nh.gov/online/BusinessInquire/LandingPageBusinessSearch'
self.browser = None
self.browser_running = False
self.page = None
def find_page(self, pageno):
if not self.browser_running:
self.start_browser()
if pageno == 1:
self.browser.get(self.NewHampshireBusinessListing)
try:
elements = [my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(self.browser, 5). \
until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".pageinfo")))]
print(f"found: {elements}")
self.page = self.browser.page_source
except TimeoutException:
print("Query timed out")
else:
self.browser.find_element(By.CSS_SELECTOR, "li.next:nth-child(9) > a:nth-child(1)").click()
try:
expected_text = "Page {pageno} of"
elements = [my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(self.browser, 5). \
until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".pageinfo")))]
print(f"Length elements: {len(elements)}")
print(f"elements: {elements}")
self.page = self.browser.page_source
except TimeoutException:
print("Query timed out")
def start_browser(self):
caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
self.browser = webdriver.Firefox(capabilities=caps)
self.browser_running = True
def stop_browser(self):
self.browser.close()
self.browser_running = False
def main():
bcs = ByCSS_SELECTOR()
bcs.start_browser()
bcs.find_page(1)
bcs.find_page(2)
if bcs.browser_running:
bcs.stop_browser()
if __name__ == '__main__':
main()
