python - Using selenium webdriver, how to click on multiple random links in webpage one after another continuously to detect broken links? -
i'm trying write test script test visible links randomly rather explicitly specifying them, in webpage upon login. possible in selenium ide/webdriver, , if how can this?
links = driver.find_element_by_tag_name("a") list = links[randint(0, len(links)-1)]
the above fetch links in first page how go testing or many links possible without manually adding above code each link/page? suppose i'm trying find broken links result in 500/404s. productive way of doing this? thanks.
currently, can't status code legitimately selenium. use selenium crawl urls, , other library requests check link's status (or use solution title check proposed @mrti):
import requests def find_broken_links(root, driver): visited = set() broken = set() # use queue bfs, list / stack dfs. elements = [root] session = requests.session() while len(elements): el = elements.pop() if el in visited: continue visited.add(el) resp = session.get(el) if resp.status_code in [500, 404]: broken.add(el) continue driver.get(el) links = driver.find_element_by_tag_name("a") link in links: elements.append(link.get_attribute('href')) return broken
Comments
Post a Comment