在迭代期间无法获取所有必需的链接 - Selenium Python

时间:2016-03-25 15:27:22

标签: python selenium exception while-loop iteration

我是Selenium Python的新手。我正在尝试获取每页10个的配置文件URL。在不使用 <?php if(isset($_POST['submit'])) { $kitkeresek = $_POST['goto']; $becsuletemw = "SELECT * FROM adatok WHERE nev = '$kitkeresek'"; $becsuletem2w = mysql_query($becsuletemw); while( $becsuletem3w = mysql_fetch_array($becsuletem2w)) $becsuletemw = $becsuletem3w["becsulet"]; $valllamiw = mysql_query("SELECT becsulet FROM adatok WHERE becsulet > '$becsuletemw' "); $rowsw = mysql_num_rows( $valllamiw ); $kitkeresekw = $rowsw + 1 ; $intvizsgalat= $kitkeresekw/10; if (is_int($intvizsgalat)) { $pagenumber = $intvizsgalat - 1 ; } else {$pagenumber = floor($kitkeresekw/10); } ; } ?> 的情况下,我可以获取所有10个网址,但仅针对第一页。当我使用while时,它会迭代,但每页只提取3或4个URL。

我需要获取所有10个链接并继续遍历页面。我想,我必须对while

做点什么

请帮我解决这个问题。

鉴于以下代码。

StaleElementReferenceException

如果有其他有效方法可以获取所需的个人资料网址并继续浏览网页,请告知我们。

1 个答案:

答案 0 :(得分:0)

我创建了一个类似的设置,对我来说没问题。我有一些问题,selenium试图点击下一个按钮,但它抛出了一个WebDriverException,可能是因为下一个按钮不在视图中。因此,不是单击下一个按钮,而是获取其href属性并使用driver.get()加载新页面,从而避免实际单击使测试更稳定。

def test_fetch_google_links():

    links = []

    # Setup driver
    driver = webdriver.Firefox()
    driver.implicitly_wait(10)
    driver.maximize_window()

    # Visit google
    driver.get("https://www.google.com")

    # Enter search query
    search_data = driver.find_element_by_name("q")
    search_data.send_keys("test")

    # Submit search query
    search_button = driver.find_element_by_xpath("//button[@type='submit']")
    search_button.click()

    while True:
        # Find and collect all anchors
        anchors = driver.find_elements_by_xpath("//h3//a")
        links += [a.get_attribute("href") for a in anchors]

        try:
            # Find the next page button
            next_button = driver.find_element_by_xpath("//a[@id='pnnext']")
            location = next_button.get_attribute("href")
            driver.get(location)

        except NoSuchElementException:
            break

    # Do something with the links
    for l in links:
        print l

    print "Found {} links".format(len(links))

    driver.quit()