How to check if a web element is visible How to check if a web element is visible selenium selenium

How to check if a web element is visible


AFAIK, BeautifulSoup will only help you parse the actual markup of the HTML document anyway. If that's all you need, then you can do it in a manner like so (yes, I already know it's not perfect):

from bs4 import BeautifulSoupsoup = BeautifulSoup(html_doc)def is_visible_1(link):    #do whatever in this function you can to determine your markup is correct    try:        style = link.get('style')        if 'display' in style and 'none' in style:#or use a regular expression            return False    except Exception:        return False    return Truedef is_visible_2(**kwargs):    try:        soup = kwargs.get('soup', None)        del kwargs['soup']        #Exception thrown if element can't be found using kwargs        link = soup.find_all(**kwargs)[0]        style = link.get('style')        if 'display' in style and 'none' in style:#or use a regular expression            return False    except Exception:        return False    return True#checks links that already exist, not *if* they existfor link in soup.find_all('a'):    print(str(is_visible_1(link)))#checks if an element existsprint(str(is_visible_2(soup=soup,id='someID')))

BeautifulSoup doesn't take into account other parties that will tell you that the element is_visible or not, like: CSS, Scripts, and dynamic DOM changes. Selenium, on the other hand, does tell you that an element is actually being rendered or not and generally does so through accessibility APIs in the given browser. You must decide if sacrificing accuracy for speed is worth pursuing. Good luck! :-)


try with find_elements_by_xpath and execute_script

from selenium import webdriverdriver = webdriver.Chrome()driver.get("https://www.google.com/?hl=en")links = driver.find_elements_by_xpath('//a')driver.execute_script('''    var links = document.querySelectorAll('a');    links.forEach(function(a) {        a.addEventListener("click", function(event) {            event.preventDefault();        });    });''')visible = []hidden = []for link in links:    try:        link.click()        visible.append('{} => Visible'.format(link.text))    except:        hidden.append('{} => Hidden'.format(link.get_attribute('textContent')))    #time.sleep(0.1)print('\n'.join(visible))print('===============================')print('\n'.join(hidden))print('===============================\nTotal links length: %s' % len(links))driver.execute_script('alert("Finish")')