0

我已经尝试了下面给出的代码,但每次我运行代码时,都会添加一些链接丢失。我想在列表中获取页面中的所有链接,以便我可以使用切片转到我想要的任何链接。

links = []
eles = driver.find_elements_by_xpath("//*[@href]")

for elem in eles:#
    url = elem.get_attribute('href')
    print(url)
    links.append(url)

有什么方法可以获取所有元素而不会丢失任何元素。

4

1 回答 1

0

sometimes the links reside inside the frames. Search for the frames in your website using inspect. so you need to switch the frame first

browser.switch_to.frame("x1")
links = []
eles = driver.find_elements_by_xpath("//*[@href]")

for elem in eles:#
    url = elem.get_attribute('href')
    print(url)
    links.append(url)
browser.switch_to.default_content()
于 2018-07-11T11:23:50.343 回答