-1

I'm using selenim python,and want to locate the following element:

<div id="coption5" class="copt" style="display: block;">

I need both the class name 'copt' and style value "display: block;",is there any way I can locate this element with both class name and attribute value at the same time?

Thanks!

William
  • 3,724
  • 9
  • 43
  • 76

1 Answers1

1

Incase considering the style value display: block; is mandatory you can induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.copt[id^='coption']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='copt' and starts-with(@id, 'coption')]")))
    
  • 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
  • Can I code like this: element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='copt' and @style='"display: block;"']"))) – William Jan 08 '20 at 20:58
  • @William `style='"display: block;"` will be taken taken care by `visibility_of_element_located`. Checkout the updated answer and let me know the status. – undetected Selenium Jan 08 '20 at 20:59
  • Hi sir ,can you help me with this one https://stackoverflow.com/questions/59654630/selenium-python-failed-to-locate-descendant-element-in-xpath – William Jan 08 '20 at 22:56
  • Hi Sir,when use element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='copt' and starts-with(@id, 'coption')]"))) I received a message: raise TimeoutException(message, screen, stacktrace) – William Jan 10 '20 at 18:11