In python selenium, how does one find the visibility of an element?

前端 未结 2 1393
半阙折子戏
半阙折子戏 2021-02-02 05:55

I found the is_visible method in the Selenium documentation, but I have no idea how to use it. I keep getting errors such as is_visible needs a selenium instance as the fi

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 06:33

    You should use is_displayed() instead:

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.get('http://www.google.com')
    element = driver.find_element_by_id('gbqfba') #this element is visible
    if element.is_displayed():
      print "Element found"
    else:
      print "Element not found"
    
    hidden_element = driver.find_element_by_name('oq') #this one is not
    if hidden_element.is_displayed():
      print "Element found"
    else:
      print "Element not found"
    

提交回复
热议问题