简体   繁体   中英

Fetch all links using Selenium Python

In a webpage there are 15 links that starts with similar link names which are inside an 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. 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.
    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")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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