使用selenium和phantomjs从两个dropbox的所有组合加载内容

2024-06-25 22:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份工作,我想通过Selenium和HhantomJS使用python2.7绑定实现自动化。问题是我尝试自动化的网站有两个Dropbox。第一个dropbox加载第二个dropbox的内容,第二个dropbox依次加载网站上的内容。你知道吗

我想得到所有这些的组合。所以我写了以下代码:

with contextlib.closing(webdriver.PhantomJS(phantomjs)) as driver:
driver.get(URL)
soup = BeautifulSoup(driver.page_source,"lxml")
firstmenu = driver.find_element_by_name("ctl00$Body$Browse1$ddlFosList");
firstmenuoptions = firstmenu.find_elements_by_tag_name('option')
firstmenuoptionsiter =iter(firstmenuoptions)
next(firstmenuoptionsiter)
for firstmenuoption in firstmenuoptionsiter:
    firstmenuoption.click()
    wait = ui.WebDriverWait(driver, 60)
    wait.until(lambda driver: driver.find_element_by_name("ctl00$Body$Browse1$ddlFosList"))
    secondmenu = driver.find_element_by_id("ctl00_Body_Browse1_ddlCourseBlockList")
    secondmenuoptions = secondmenu.find_elements_by_tag_name('option')
    for secondmenuoption in secondmenuoptions:
        print secondmenuoption.text

但是,我在打印时遇到了一个StaleElementReference异常secondmenuoption.text线路。这可能是因为选中第一个菜单时页面会重新加载。关于如何进行有什么想法吗?你知道吗


Tags: name内容by网站driverbodyelementfind
1条回答
网友
1楼 · 发布于 2024-06-25 22:45:27

我在StaleElementReference异常方面遇到了一些问题,我能想到的最佳解决方案是添加线程。睡眠(). 您不能等待某个预期条件,因为第二个dropbox中的所有元素都已加载。你知道吗

Selenium确实有刷新的函数(ExpectedCondition),所以您可以尝试一下。否则,我认为线程。睡眠()是一个有效(但不好)的解决方案。你知道吗

相关问题 更多 >