使用Selenium for python

时间:2017-07-27 10:29:31

标签: selenium

我尝试使用以下代码从网站复制文字,我希望它能够自动点击" NEXT"表格末尾的按钮,没有点击它,代码工作得很好但是当我添加最后一行来点击它时会出现错误:

  

"消息:元素不再附加到DOM Stacktrace:at   fxdriver.cache.getElementAt   (来源://fxdriver/modules/web-element-cache.js:9354)       在Utils.getElementAt(file:/// c:/users/user/appdata/local/temp/tmpa7dvts/extensions/fxdriver@googlecode.com/components/command-processor.js:8978)       at WebElement.getElementText(file:/// c:/users/user/appdata/local/temp/tmpa7dvts/extensions/fxdriver@googlecode.com/components/command-processor.js:11965)       在DelayedCommand.prototype.executeInternal_ / h(file:/// c:/users/user/appdata/local/temp/tmpa7dvts/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)       在DelayedCommand.prototype.executeInternal_(file:/// c:/users/user/appdata/local/temp/tmpa7dvts/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)       在DelayedCommand.prototype.execute /< (文件:/// C:/users/user/appdata/local/temp/tmpa7dvts/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)"

我使用的代码是:

driver = webdriver.Firefox()
driver.get("https://it.website.com/Statistics")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
Next_first = 0
Next_first = driver.find_elements_by_id("next")[0]
data_vector [i]=driver.find_elements_by_class_name("tn")[i].text
Next_first.click()

虽然网站代码是:

<tr>
  <td class="tn"> text
  </td>
  <table class="table class" id="table id">
    <thead>...code for table title...
    </thead>
    <tbody>
      <tr>
        <td class="tn">data
        </td>
      </tr>
    </tbody>
    <dd>
      <a class="option clickable" id="next" data-page="1">NEXT</a>
    </dd>
</tr>

1 个答案:

答案 0 :(得分:0)

是否有更多的html源代码,比如javascript? 因为输入可以通过javascript分离,这意味着你必须等待它正确加载(driver.implicitly_wait(#seconds)

here指示&#34;隐含等待&#34;的描述的链接

第二个解决方案可能是您必须在测试前清除webdriver的缓存/ cookie,如here所述