1

I am am trying to execute javascript functions inside anchor tags with selenium and trying to store that result somewhere

HTML code:

<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>

Below is my code

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

Here i am able to get the href from the anchor tags but when i try to execute like shown above it is showing the below error. And when i used click() function like

for anchor_tag in anchor_tags:
      anchor_tag.click()

The javascript is executing fine, but i want to store the result of the execution, because in the code above the javascript function is nothing but which generates pagination

Result:

---------------------------------------------------------------------------
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"}}'

So how to store the result after executing javascript in selenium

Thanks in advance........................

Edited Code:

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"

I edited code and i had run this code from a file through terminal, i am not getting any errors, but the result is none. Actually i want to save the result after javascript execution.Please let me know why the result is empty ..............

Thanks in advance................

Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • what do you see in a browser when you click the link? What result do you expect? For example, if javascript changes the document then just query the DOM as usual to get the result. – jfs Jun 27 '12 at 11:44
  • we can get the result by what javascript does by viewing in firebug, but i want that through selenium, as u see above the url "http://www.e3050.com/Items.aspx?cat=SON" i want to achieve pagination actually, but it is done through javascript functions, so when we click on the next page dynamically data is loading on to the age without changing the url, so i want to use selenium and find out(catch the result) what happens when we click on the pagination i mean store the result of the javascript fucntion probably a url or something, if something wrong in my explanation please let me know – Shiva Krishna Bavandla Jun 27 '12 at 12:00

1 Answers1

2

To get the result after the click, just call an appropriate find_element_by* method. You might need selenium.webdriver.support.ui.WebDriverWait to wait for the results to load.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thanks thats useful, if u don't mind can u please once show that in my edited code above to print the result "element" – Shiva Krishna Bavandla Jun 27 '12 at 13:08
  • can u provide me an example of that so that it will be more helpful. Thanks in advance – Shiva Krishna Bavandla Jun 27 '12 at 13:15
  • @ J.F. Sebastian: That some what helped, sorry in the above mentioned example the intesnion is to save the url generated by the javascript execution, but there the entire page source is printed and what about the url of the page source(what i need exactly). In my example as u indicated i am able to execute the javascript function and page moved from 1 to 2(trying to achieve pagination), here i want to find the url of the 2nd page. How can we do this with the above example – Shiva Krishna Bavandla Jun 28 '12 at 05:51
  • @shivakrishna: the URL as you've mentioned in the comment to your answer is the same (it doesn't change). It seems "the result" is that JavaScript changes the page you're already on i.e., it doesn't return any URL. If you don't need the source of the full page then you could use `find_element_by*()` to get intersting bits. – jfs Jun 28 '12 at 12:49
  • yes u are right the url doesn't change its dynamically changing the values on to the page, but how to solve this in case of pagination, i mean is there a way to achive pagination in the above mentioned url? – Shiva Krishna Bavandla Jun 28 '12 at 13:16
  • @shivakrishna: just follow the steps as if you would do it manually in a browser: open a page (.get), locate a link (.find_element), click it (.click), wait while the page updates (webdriverwait), read interesting bits (.find_element), click another link (.click), etc. – jfs Jun 28 '12 at 13:36