I would like vba to scroll a pdf-loaded webpage to page5 and do screencapture.
I tried ExecuteScript, not working in this case.
It only works in normal webpages, such as yahoo.com
does anyone know how to scroll the webpage down using chromedriver?? Thanks!!
https://www.spf.com.tw/upload/sinopac/researchContent/184f5b44c0b000007e04.pdf
Sub screencapture_pdfpages()
Dim cd As Selenium.ChromeDriver
Set cd = New Selenium.ChromeDriver
cd.AddArgument "start-maximized"
cd.Start
cd.Wait 1000
cd.Get "https://www.spf.com.tw/upload/sinopac/researchContent/184f5b44c0b000007e04.pdf"
cd.Wait "9000"
' not working
cd.ExecuteScript ("window.scrollTo(0, document.body.scrollHeight);") 'full pagedown not work
cd.ExecuteScript ("window.scrollTo(0, 510);") 'scroll not work
end sub
Related
I’m trying to do web automation on IE using Selenium library.
Elements I want to click have iframes.
However, I’m getting the “element not found” error.
I tried many different approaches.
Sub activeBexIE_Final()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim Perm_bot As New Selenium.IEDriver
Perm_bot.Get "https://nalcodirect.nalco.com/irj/servlet/prt/portal/prtroot/pcd%213aportal_content%212fcom.sap.pct%212fplatform_add_ons%212fcom.sap.ip.bi%212fiViews%212fcom.sap.ip.bi.bex?BOOKMARK=0O867HQNBT13YLB5Q21HLAL2H", timeout:=1000, Raise:=False
Perm_bot.Wait 5000
' website opening successfully
Perm_bot.FindElementById("logonuidfield").SendKeys "XYZ#ABC.com"
Perm_bot.SendKeys Perm_bot.Keys.Tab
Perm_bot.SendKeys "ABCD"
Perm_bot.SendKeys Perm_bot.Keys.Enter
' the error is element not found at .click line
Perm_bot.switchToFrame Perm_bot.FindElementByName("iframe_Roundtrip_9223372036563636042")
Perm_bot.FindElementById("BUTTON_OPEN_SAVE_btn1_acButton").Click
Perm_bot.Wait 5000
Application.DisplayAlerts = True
Application.ScreenUpdating = True
perm_dot.Quit
Set perm_dot = Nothing
End Sub
HTML:
Iframe Screenshot:
Element Screenshot:
It might because the script runs faster than the page load. You could add a wait before switch to the iframe. Besides, there might be other elements with name iframe_Roundtrip_9223372036563636042 so you could use FindElementById when searching the iframe.
I made a simple sample with code below and it can work well:
Public Sub seleniumtutorial()
Dim driver As New Selenium.IEDriver
driver.Get "https://zhouyuzy.github.io/create-test-page/iframe.html "
driver.Wait 5000
driver.SwitchToFrame driver.FindElementById("iframe_Roundtrip_9223372036563636042")
driver.FindElementById("BUTTON_OPEN_SAVE_btn1_acButton").Click
driver.Wait 5000
End Sub
The result is like below:
I'm trying to click on the Load More button located at the bottom of the left window of this webpage using vba in combination with selenium but the script always throws timeout error pointing at this .Get Url line. Although it seems I've defined an accurate xpath to locate the element, I can't think further as to what I should do now to achieve the same.
How can I click on that Load More button?
Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.Get Url
Set post = .FindElementByXPath("//div[contains(.,'Load More')]")
.ExecuteScript "arguments[0].scrollIntoView();", post
post.Click
End With
End Sub
I see two "Load More" buttons. Both are matched by "//div[contains(.,'Load More')]". The first one is hidden. You need to handle second one.
Try this XPath
"//div[#class='content' and . = 'Load More']"
At least for me there were a couple of banners to dismiss as well as scrolling. There was no problem with the get line
Option Explicit
Public Sub ClickOnLoadMore()
Const Url$ = "http://www.ratemyprofessors.com/search.jsp?queryoption=TEACHER&queryBy=schoolDetails&schoolID=457&schoolName=James+Madison+University&dept=select"
Dim driver As New ChromeDriver, post As Object
With driver
.get Url
If .FindElementsByCss(".close-notice.close-this").Count > 0 Then
.FindElementByCss(".close-notice.close-this").Click
End If
.SwitchToFrame .FindElementByCss("[id^='spout-unit-iframe']")
With .FindElementByCss("#spout-ads #spout-header-close")
.ScrollIntoView
.Click
End With
.SwitchToDefaultContent
.ExecuteScript "document.querySelector('.result-list [onclick*=LoadMore]').scrollIntoView(true);" & _
"window.scrollBy(0, -(window.innerHeight - this.clientHeight) / 2);"
.FindElementByCss(".result-list [onclick*=LoadMore]").Click
Stop '<== Delete me later
'other code
.Quit
End With
End Sub
I've written a script using VBA in combination with selenium to get all the company links from a webpage which doesn't display all the links until scrolled downmost. However, when I run my script, I get only 20 links but there are 1000 links in total. I've heard that it is possible to accomplish this type of task executing javascript function between the code. At this point, I can't get any idea how can I place that within my script. Here is what I've tried so far:
Sub Testing_scroll()
Dim driver As New WebDriver
Dim posts As Object, post As Object
driver.Start "chrome", "http://fortune.com/fortune500"
driver.get "/list/"
driver.execute_script ("window.scrollTo(0, document.body.scrollHeight);") --It doesn't support here
Set posts = driver.FindElementsByXPath("//li[contains(concat(' ', #class, ' '), ' small-12 ')]")
For Each post In posts
i = i + 1
Cells(i, 1) = post.FindElementByXPath(".//a").Attribute("href")
Next post
End Sub
According to the examples included with SeleniumBasic you should be using
driver.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);")
not "driver.execute_script", which is the python equivalent from the previous solution I gave you :) You are going to have to loop that in the same way until you've got all 1000 links on the page.
This works for me. It scrolls incrementally down the page until it reaches the end. I found that scrolling directly to the end didn't load all of the in-between elements. This example is Selenium VBA for a Chrome driver.
Sub scrollToEndofPage(dr As WebDriver)
Dim aCH As WebActionChain, scrPOS As Long, oldPOS As Long
scrPOS = dr.ExecuteScript("return window.pageYOffset;")
Set aCH = dr.ActionChain
oldPOS = -1
Do While scrPOS > oldPOS
oldPOS = scrPOS
aCH.ScrollBy 0, 100: aCH.Perform
scrPOS = dr.ExecuteScript("return window.pageYOffset;")
DoEvents
Loop
End Sub
I was searching a lot on the web and found your website very helpfull - that is why I have not posted anything yet, just reading.
However, I need your advice in the following step of my project. The porject is to automate the creation/placing of ad for selling goods.
The website is http://olx.bg/adding/
What I am failing to do through VBA is to press the blue square called "ДОБАВИ СНИМКА" which stands for "ADD PHOTO" in the form.
Once pressed a default BROWSE window opens to select the file.
Thanks in advance!
Add two references to your VBA project:
Microsoft Internet Controls
Microsoft HTML Object Library
The HTML behind the button has an id = add-files which makes it pretty easy to locate using getElementById
Here is a working example:
Sub PushAddPhotoButton()
Dim IEexp As InternetExplorer
Set IEexp = New InternetExplorer
IEexp.Visible = True
IEexp.navigate "http://olx.bg/adding/"
Do While IEexp.readyState <> 4: DoEvents: Loop
Dim photoButton As HTMLInputElement
Set photoButton = IEexp.Document.getElementById("add-files")
If (Not photoButton Is Nothing) Then
photoButton.Click
Else
MsgBox "Button not found on web page."
End If
IEexp.Quit
Set IEexp = Nothing
End Sub
If your IE security settings are on 'High' for Internet then you won't get a dialog or an error. The IE window will simply flash and then be gone.
Change to:
I saw a similar thread on Java but I am not familiar with that language so need a solution for VBA- suppose I want to press ctrl+A to select all content in a website using selenium:
Private Sub CommandButton1_Click()
Dim shell: Set shell = CreateObject("WScript.Shell")
Dim driver As New SeleniumWrapper.WebDriver
driver.Start "firefox", "http://www.espnfc.com/gamecast/statistics/id/376930 /statistics.html"
driver.setImplicitWait 5000
driver.Open "/"
shell.SendKeys "^a"
End sub
However this only highlights the URL. What is the best way to select all the content in the webpage and then copy it (ctrl +a and ctrl+ c)? ALso are there any references I need?
I've been looking for a good guide to selenium keys on google but have been struggling so far...
Have you tried .getBodyText? I tested it once and it worked fine:
Private Sub CommandButton1_Click()
Dim shell: Set shell = CreateObject("WScript.Shell")
Dim driver As New SeleniumWrapper.WebDriver
dim Body as string
driver.Start "firefox", "http://www.espnfc.com/gamecast/statistics /id/376930 /statistics.html"
driver.setImplicitWait 5000
driver.Open "/"
Body=driver.getBodyText
End sub
You can then 'paste' into a spreadsheet via cells(1,1)=Body or back into a webpage element via driver.findElementById("ElementNameHere").SendKeys Body