0

I am trying to read the webpage where after selecting the dropdown menu, the message is displayed. When the message appears it has class

<div class="alert alert-info border-0 rounded-0"> No update currently available </div>

I wrote the following code to read the text but always getting an exception

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

alert = WebDriverWait(driver, 5).until(EC.alert_is_present)
print(alert.text)

at the line with webdriver wait, I am getting the below error

__init__() takes 1 positional argument but 2 were given

My goal is to read the text in the alert class and validate it further. Any help will be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

Though the WebElement's class attribute contains the value alert still it isn't a JavaScript enabled Alert and you have to deal with it as a regular WebElement.

To print the text No update currently available you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element_by_css_selector("div.alert.alert-info").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//div[contains(@class, 'alert') and contains(@class, 'alert-info')]").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.alert.alert-info"))).text)
    
  • Using XPATH and get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'alert') and contains(@class, 'alert-info')]"))).get_attribute("innerHTML"))
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352