Reputation: 49
I'm trying to download documents from a website. When I inspect the element in my browser, this is what I get:
<td width="3%" align="left" id="tdvPDF0" colspan="3">
<a href="#" onclick="javaScript:downloadClicked('JRAOB2SNRXEAPX2', 0, 'PDF'); return false;" onmouseover="window.status='CTNF'; return true" onmouseout="window.status=''; return true">PDF</a>
/
<a href="#" onclick="javaScript:downloadClicked('JRAOB2SNRXEAPX2', 0, 'XML_LINK'); return false;" onmouseover="window.status='CTNF'; return true" onmouseout="window.status=''; return true">XML</a>
/
<a href="#" onclick="javaScript:downloadClicked('JRAOB2SNRXEAPX2', 0, 'DOCX'); return false;" onmouseover="window.status='CTNF'; return true" onmouseout="window.status=''; return true">DOCX</a>
</td>
I would like download all three documents, i.e. the PDF, XML, and DOCX. This JavaScript can accept three arguments. In this case they are: 1. JRAOB2SNRXEAPX2 (string) 2. 0 (integer) 3. PDF (string)
I have no idea of how to ascertain the correct input for the first argument (in this example: "JRAOB2SNRXEAPX2")
I would like to have my code work regardless of the first argument.
Previously, when I've encountered JavaScript functions I used the following:
driver.execute_script(name_of_JavaScript_script())
that would generally work, however I have never encountered a JavaScript with arguments as in this case, e.g. downloadClicked('JRAOB2SNRXEAPX2', 0, 'PDF')
I tried the following without success:
driver.execute_script(downloadClicked('JRAOB2SNRXEAPX2', 0, 'PDF'))
driver.execute_script(downloadClicked(''JRAOB2SNRXEAPX2', 0, 'PDF''))
driver.execute_script(downloadClicked('JRAOB2SNRXEAPX2', 0, 'PDF')); return false;
and many other similar options.
I've also tried:
javascript = driver.find_element_by_id('tdvPDF0').click()
driver.execute_script(javascript)
In addition I've tried:
driver.find_element_by_id('tdvPDF0').click()
The code for the function currently looks like this:
def private_pair_ifw_downloader(driver, application_number, pause=1):
private_pair_enter_application(driver, application_number)
time.sleep(pause)
driver.execute_script('submitTab("ifwtab")')
time.sleep(pause)
driver.execute_script('"javaScript:downloadClicked(''JRAOB2SNRXEAPX2', 0, 'PDF''); return false;"')
I expect for the code to invoke the JavaScript function which in turn should download the PDF file however, I received the following error:
Traceback (most recent call last): File "C:/Workspaces/patents_repo/USPTO_scraper/uspto_private_pair_scraper.py", line 41, in private_pair_ifw_downloader(driver, '15723211') File "C:\Workspaces\patents_repo\utils\web_utils.py", line 211, in private_pair_ifw_downloader driver.execute_script('"javaScript:downloadClicked(''JRAOB2SNRXEAPX2', 0, 'PDF''); return false;"') File "C:\Users\eitan\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script 'args': converted_args})['value'] File "C:\Users\eitan\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\eitan\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: SyntaxError: Unexpected identifier (Session info: chrome=76.0.3809.100)
Upvotes: 0
Views: 3764
Reputation: 54984
If you don't know the string you can try something like:
driver.execute_script('document.querySelector("a[onclick*=PDF]").onclick()')
Upvotes: 0
Reputation: 14135
All you have to do is wrap your input to execute_script in double quotes.
driver.execute_script("downloadClicked('JRAOB2SNRXEAPX2', 0, 'PDF');")
Upvotes: 1