How to call Robot.ExecuteScript to
click a button? So far I have used Python for web robots, but yesterday I got the WebDriver4L demo working under Linux and can't resist trying to switch!
Here is my working Python code
DownloadButton = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div/button')))
browser.execute_script("arguments[0].click();", DownloadButton)
My attempt with WebDriver4L. I confess to not knowing exactly what I'm doing here..
var
DownloadButton: TWebElement;
begin
DownloadButton:= Robot.FindElementByXPath('/html/body/div[2]/div/button');
Robot.ExecuteScript('DownloadButton', 'arguments[0].click();')
And the error message:
Unexpected token (arguments) encountered
EDIT: Found an example that clicks the button and completes the download, but the downloaded textfile is garbled and unreadable for some reason?
Robot.ExecuteScript ( 'var xpath = "/html/body/div[2]/div/main/div[2]/article/div[3]/div[1]/div/div/div[3]/div[2]/div[2]/div/button";' +
'function getElementByXpath(path) {' +
'return document.evaluate(' +
' path,' +
' document,' +
' null,' +
' XPathResult.FIRST_ORDERED_NODE_TYPE,' +
' null' +
').singleNodeValue;' +
'}' +
'var element = getElementByXpath(xpath);' +
'if (!element) {' +
'throw new Error("Error: cannot find an element with XPath(" + xpath + ")");' +
'}' +
'element.click();' ) ;
code is borrowed from here:
http://www.tauhidslab.my.id/2024/02/scrape-data-dari-website-kpu-dengan.htmlEDIT2:
After checking the downloaded .CSV file more closely, I found that it was just in LibreOffice the content seemed garbled. CudaText, MousePad etc showed it just fine. It turned out to be US-ASCII encoding. I converted the file to UTF8 instead and LibreOffice read it fine too.