我正在尝试使用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执行后保存结果。请让我知道为什么结果是空的..............
提前致谢................
答案 0 :(得分:2)
要在点击后获得结果,只需调用适当的find_element_by*
方法即可。您可能需要selenium.webdriver.support.ui.WebDriverWait
等待结果加载。