1

I want to find with Selenium (python) an id based on a class tag

I want to find the result of a html form. There are 2 results possible, available or not available. Both options will appear in the html code, but two different CSS format will be used to show the result and to hide the other option.

HTML CODE

I would like to get the id (either "WarningDisponible" or "WarningIndisponible") based on the div class "Content Warning" (this is the result), "Content Warning hidden" is not the result.

cottontail
  • 10,268
  • 18
  • 50
  • 51
olivier
  • 31
  • 3

3 Answers3

0

This website should help: https://selenium-python.readthedocs.io/locating-elements.html Search the div first with ContentWarning = driver.find_element_by_class_name('Content Warning') From there you can just search within the div by checking if ContentWarning.find_element_by_id('WarningDisponible') returns any results.

This does assume that there are no other elements in the HTML with the class name 'Content Warning' other than the div you are looking for.

Finlay
  • 108
  • 10
  • 1
    Thanks. There is indeed only one tag "Content Warning". would there be a way to get directly the name of the id rather than testing for the existence of 1 (to derive the result)? – olivier Nov 29 '21 at 06:22
  • Try `p_element = contentWarning.find_element_by_tag_name('p')` and then `p_element.get_attribute('id')` – Finlay Nov 29 '21 at 06:36
0

Another simple way is to give complete xpath. This way, you can avoid confusion

Select(driver.find_element(by=By.XPATH,value='your_complete_xpath')

and then as per your need send_keys() or select_by_value("your_value_here").

cottontail
  • 10,268
  • 18
  • 50
  • 51
jango
  • 1
-1

To print the value of the id attribute i.e. WarningIndisponible based on the classname Content warning you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "div.Content.warning > p").get_attribute("id"))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//div[@class='Content warning']/p").get_attribute("id"))
    

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:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.Content.warning > p"))).get_attribute("id"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='Content warning']/p"))).get_attribute("id"))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Many thanks for your detailed answer. A feedback on the 2 suggested methods: XPATH works perfectly CSS does not work as it seems it does not need a perfect match. As the 2 tags are "ContentWarning" or "ContentWarning Hidden" and the latter is alway on top, it shows always the latter as the first result. In any case, this is solved. THANKS – olivier Nov 29 '21 at 21:42
  • @olivier yeah, tell me, do you need any further help? – undetected Selenium Nov 29 '21 at 21:44
  • @Debanjay by chance, would you know the same instruction in javascript. The code is too slow with selenium, I would like to inject javascript code in the page with Code injector but I have the same issue to find the appropriate tag. Thanks again – olivier Nov 30 '21 at 05:53