1

我正在尝试使用 selenium 在锚标记内执行 javascript 函数,并尝试将结果存储在某处

HTML 代码

<td>
<td>
  <ul class="compare">
    <li class="floatRight dividLineLeft category_page_navigation">
       <div class="page_navigation">
           <span id="ctl00_ctl00_ContentPlaceHolder1_ItemListPlaceHolder_pager1">
               <a style="width: 22px; height: 22px;" disabled="disabled">
               <a style="width: 22px; height: 22px;" href="javascript:__doPostBack('ctl00$ctl00$ContentPlaceHolder1$ItemListPlaceHolder$pager1$ctl00$ctl01','')">
           </span>
       </div>
    </li>
  </ul>
</td>

下面是我的代码

from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get("http://www.e3050.com/Items.aspx?cat=SON");
anchor_tags = driver.find_element_by_xpath("//div[@class='page_navigation']/span[@id='ctl00_ctl00_ContentPlaceHolder1_ItemListPlaceHolder_pager1']")
anchor_tags_res = anchor_tags.find_elements_by_tag_name("a")
for anchor_tag in anchor_tags_res:
      href=[]
      href.append(anchor_tag.get_attribute("href"))
      for i in href:
          element = driver.execute_script(i)
          print element

在这里,我可以从锚标签中获取 href,但是当我尝试像上面显示的那样执行时,它会显示以下错误。当我使用 click() 函数时

for anchor_tag in anchor_tags:
      anchor_tag.click()

javascript执行良好,但我想存储执行结果,因为在上面的代码中,javascript函数只不过是生成分页

结果:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)

/home/local/<ipython console> in <module>()

/usr/lib/python2.7/site-packages/selenium-2.21.3-py2.7.egg/selenium/webdriver/remote/webdriver.pyc in execute_script(self, script, *args)
    383         converted_args = list(args)
    384         return self.execute(Command.EXECUTE_SCRIPT,
--> 385             {'script': script, 'args':converted_args})['value']
    386 
    387     def execute_async_script(self, script, *args):

/usr/lib/python2.7/site-packages/selenium-2.21.3-py2.7.egg/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
    151         response = self.command_executor.execute(driver_command, params)
    152         if response:
--> 153             self.error_handler.check_response(response)
    154             response['value'] = self._unwrap_value(
    155                 response.get('value', None))

/usr/lib/python2.7/site-packages/selenium-2.21.3-py2.7.egg/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
    123             if exception_class == ErrorInResponseException:
    124                 raise exception_class(response, value)
--> 125             raise exception_class(value)
    126         message = ''
    127         if 'message' in value:

WebDriverException: Message: '{"sessionId":"ed4b6f549a9902254bc49399cddbe79a","status":400,"value":{"message":"No script specified"}}'

那么在 selenium 中执行 javascript 后如何存储结果

提前致谢........................

编辑代码

from selenium import webdriver
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get("http://www.e3050.com/Items.aspx?cat=SON");
anchor_tags = driver.find_element_by_xpath("//div[@class='page_navigation']/span[@id='ctl00_ctl00_ContentPlaceHolder1_ItemListPlaceHolder_pager1']")
all_options = anchor_tags.find_elements_by_tag_name("a")
hrefs = all_options[1].get_attribute("href")
res = hrefs.encode('utf-8')
element = driver.execute_script('%s'%res)
WebDriverWait(driver, timeout=10).until(lambda x: x.find_element_by_id('ctl00_ctl00_ContentPlaceHolder1_ItemListPlaceHolder_itemslv_ctrl0_Counterlbl'))
print element,"Here should be the result"

我编辑了代码,并通过终端从文件中运行了此代码,我没有收到任何错误,但结果没有。实际上我想在 javascript 执行后保存结果。请让我知道为什么结果是空的............

提前致谢................

4

1 回答 1

2

点击后要得到结果,只需调用适当的find_element_by*方法即可。您可能需要selenium.webdriver.support.ui.WebDriverWait等待结果加载。

于 2012-06-27T12:22:40.453 回答