简体   繁体   English

使用 Selenium Python 获取所有链接

[英]Fetch all links using Selenium Python

In a webpage there are 15 links that starts with similar link names which are inside an iframe.在一个网页中,有 15 个链接以相似的链接名称开头,它们位于 iframe 中。 If I try to fetch those not all the links are fetched.如果我尝试获取那些并非所有链接都被获取。 Below are the code I tried.下面是我试过的代码。

driver = webdriver.Chrome(ChromeDriverManager().install())
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(("xpath", "//*[@id='brandBand_2']/div/iframe")))
wo_link1 = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located(("partial link text", 'SA')))
for link in wo_link1:
    print(str(link.get_attribute("href")))

If I manually zoom out the webpage to the maximum extent, then I am able to get all the links.如果我手动将网页缩小到最大程度,那么我可以得到所有的链接。 Is there are any other ways to get those links without zooming out?有没有其他方法可以在不缩小的情况下获取这些链接?

  1. In can you did not do that - maximize the screen size with你可以不这样做 - 最大化屏幕尺寸
options = Options()
options.add_argument("start-maximized")
  1. visibility_of_all_elements_located will not actually wait for all the elements matching the passed locator to be visible. visibility_of_all_elements_located实际上不会等待与传递的定位器匹配的所有元素都可见。 Actually it waits for at least 1 element matching the passed locator to be visible and then it returns all the matching elements caught at that moment.实际上,它等待至少 1 个与传递的定位器匹配的元素可见,然后返回当时捕获的所有匹配元素。
    You can use this method to wait for first element visibility, add some short delay and then grab all the matching links, as following:您可以使用此方法等待第一个元素可见,添加一些短暂的延迟,然后抓取所有匹配的链接,如下所示:
driver = webdriver.Chrome(ChromeDriverManager().install())
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it(("xpath", "//*[@id='brandBand_2']/div/iframe")))
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located(("partial link text", 'SA')))
time.sleep(1)
wo_links = driver.find_element(By.PARTIAL_LINK_TEXT, 'SA')
for link in wo_links:
    print(str(link.get_attribute("href")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM