I using VB.net working with selenium.This is my code
With New Actions(driver)
.MoveByOffset(100, 100).Click().Build().Perform() 'This code working only
End With
With New Actions(driver)
.MoveByOffset(200, 100).Click().Build().Perform()
End With
With New Actions(driver)
.MoveByOffset(300, 100).Click().Build().Perform()
End With
The code above just working for first block only, i was tried many way, if one block worked, the other block will not work anymore. I using this code for other site same type still working normally but there special site it is not work.
What should i do now :(
Related
WinAppDriver's FindElement will not always find objects in the program to be automated.
I've gotten this to work with other programs, like Notepad, and even a different dialog in my program to be automated, and it worked in those places.
This is the code I am using so far. The first three lines execute without error, successfully launching the application into it's Login dialog:
Dim appCapabilities As DesiredCapabilities = New DesiredCapabilities()
appCapabilities.SetCapability("app", "C:\[my program].exe")
Dim ProgramSession = New WindowsDriver(Of WindowsElement)(New Uri("http://127.0.0.1:4723"), appCapabilities)
ProgramSession.FindElementByName("Password").SendKeys("Password")
The fourth line should find the element, a text box, and enter the string "Password" into it via sendkeys, but it fails, with the following exception:
System.InvalidOperationException: 'An element could not be located on the page using the given search parameters.'
The target object is on screen, and this should work. I'm using the info shown for the object in Inspect.exe, Name: "Password".
WinAppDriver's window shows the following error information:
{"using":"name","value":"Password"}
HTTP/1.1 404 Not Found
Content-Length: 139
Content-Type: application/json
{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
The fourth line of code is executed directly after program startup.
Since a program needs some load time, you'll need to wait for the program to finish loading before trying to search for a control on the GUI. You can do this by using a while loop in combination with a stopwatch for timeout.
Dim shouldContinue As Boolean = True
Dim stopWatch As StopWatch = New StopWatch()
Dim timeOut As TimeSpan = TimeSpan.FromSeconds(30)
stopWatch.Start()
While shouldContinue AndAlso timeOut > stopWatch.Elapsed
If element.IsFound Then
shouldContinue = False
stopWatch.Stop()
End If
End While
element.IsFound is just mock-up code, you will need to fill in that blank. This is a good Q/A to show you how to check if a element has loaded.
Another thing you need to take in account is the possibility that your Login Dialog runs in another window handle. If the window handle winappdriver is using is different from the window handle where your element is at, you won't be able to find that element.
Also check if you can find whatever you are searching for in the PageSource property xml from your driver. I usually do this by calling that property in the visual studio watch window, and copying it's content to a xml formatter tool.
I was able to find the password field by using FindElementByXPath instead of FindElementByName.
In order to find the xpath, I used the Recorder for WinAppDriver.
These xpaths can be VERY long. I was able to shorten some of them by removing some duplicate attributes, but some are over 450 characters long. I can sometimes reduce it further with variables, but I'm not exactly delighted so far with WinAppDriver as a replacement for CodedUI.
I'm having some issue with this code
Private Sub CortarSobrantes()
'Procedimiento que llama al comando "Comprimir imágenes" con parámetros
With Application.CommandBars.FindControl(ID:=6382)
SendKeys "%T%n%C{ENTER}", False ' Las letras equivalen a los accesos de teclado en la ventana, ~ para ejecucion
.Execute
End With
End Sub
If I hit Run (F5) inside the project, it runs OK, but when I try to call it from a button shortcut it doesn't catch the SendKeys. This also happens if I try to run it step by step (F8)
BTW %T%n%C is for Spanish command combination (all images, not compress, without resolution change and deleting cropped areas)
The reason I'm using SendKeys is that I'm trying to remove cropped areas within a function in order to call it from a button, so I could skip marking the options. As far as I know, there's nothing in the object model that allows this.
Am I missing something with the focus?
Not sure what you're actually trying to achieve, but in general the SendKeys() method is usually avoided in VBA because it's unreliable and buggy at best.
SendKeys() will send virtual keystrokes to whichever window has focus at the time of execution - so timing is everything.
If you know the exact text in the caption of your window you can use the AppActivate() method to force focus just before using SendKeys()
Moreover, SendKeys() is more widely regarded as a "final attempt" or workaround because 90% of the time you can use winapi to get the same result reliably although more advanced knowledge of VBA/programming is required when using Win API
I'm also having trouble with the SendKeys-statement.
I'm trying to send Alt= to a Word document. Thereby changing some mahematical formula in an equation field. For instance: ((x_1 q+x_2 p)/(p+q),(y_1 q+y_2 p)/(p+q))
After some time I found that the SendKeys-statement works as advertised only and ONLY if there's no further statement in my macro.
I know this is a bit awkward, but there it is.
Consider the following:
Public Sub test()
Dim test As String
test = ActiveWindow.Selection.Text
SendKeys "%=", True
' MsgBox test
End Sub
This works fine.
Copy/paste the formula above in a Word-document, open VBA in Word, make a new Module and copy/paste the Sub above into it.
Select the formula.
You must start the Sub form the Macro's menu (Alt+F8).
You'll notice that the formula will change in an equation-field and gets centered in the middle.
Run the macro again and the formula is changed back in standard text-format.
Now activate the MsgBox-statement and you'll notice that the SendKeys-statement doesn't work anymore.
I know this is hardly a workaround for the problem, but maybe someone will find this information useful!
Or how to work with collections (or arrayss) in VBA.
The issue is most probably myself, but I couldn't find an answer yet.
I am trying to go trough a some pages on a web-site with Selenium-vba to find some data.
As usual if there is more to display, the site shows a 'NEXT' button. The button has <a href ... > when the link is activated, else it's just plain text.
To test if there is another page I have found the way to use findElementsByLinkText, and either there is a link or the the collection is empty. So this can be tested by the size of the collection.
This works so far.
But when I try to use the collection (aside from a for each loop) for further action I can't get it to operate.
This is the code:
Dim driver As New SeleniumWrapper.WebDriver
Dim By As New By, Assert As New Assert, Verify As New Verify, Waiter As New Waiter
On Error GoTo ende1
driver.Start "chrome", "http://www.domain.tld/"
driver.setImplicitWait 5000
driver.get "//......."
Set mynext = driver.findElementsByLinkText("Next")
if mynext.Count >0 Then
mynext(1).Click 'THIS STATEMENT DOES NOT WORK
End If
So please help me to get around my understanding issue (which I am convinced it is)
How can I access an element from the collection.
My workaround so far is to execute
driver.findElementByLinkText("Next").Click
but this is unprofessional as it executes the query again.
The Next button is probably loaded asynchonously after the page is completed.
This implies that findElementsByLinkText("Next") returns no elements at the time it's called.
A way to handle this case is to silent the error, adjust the timeout and test the returned element:
Dim driver As New Selenium.ChromeDriver
driver.Get "https://www.google.co.uk/search?q=selenium"
Set ele = driver.FindElementByLinkText("Next", Raise:=False, timeout:=1000)
If Not ele Is Nothing Then
ele.Click
End If
driver.Quit
To get the latest version in date working with the above example:
https://github.com/florentbr/SeleniumBasic/releases/latest
I have VisualBasic project wih many forms,and one of it is form1.Whenever the initializecimponent method is called,VS stops responding and i have to end it using taskmgr.The bug is inside the form1.designer.vb, because i am able to view and edit other forms and usercontrols in designer.Anoter strange thing is that the stand alone exe runs successfully outside the IDE.I also tried adding the form to another project and that project too crashes.Whenever i build/debug/view form1 in designer, i get two dialogs one after the other:VS2010 has stopped working, VS2010 is restarting( but it never does!)
Create a copy of the form, and begin removing parts of the form until it works. The last thing you removed is what failed. You'll have to do this manually by removing three parts of each control:
The Declaration (at the bottom)
The instantiation (at the top)
The configuration (in the middle). Below is an example of the configuration
'
'LeftLabel
'
Me.LeftLabel.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.LeftLabel.AutoSize = True
Me.LeftLabel.Location = New System.Drawing.Point(3, 3)
Me.LeftLabel.Name = "LeftLabel"
Me.LeftLabel.Size = New System.Drawing.Size(39, 13)
Me.LeftLabel.TabIndex = 1
Me.LeftLabel.Text = "Label2"
You'll also need to remove any references to that control, such as it being added to a container.
Do this one control at a time, then build the project and open the form. If the form does NOT open, restart visual studio and do the next control. if it DOES open, then the last thing you removed was the culprit.
For programmers facing the same problem, here is the solution:
Make sure that your user controls doesn't creates instances of themselves inside themselves.even though your program might run without any error, VS designer will crash.here is an example for such kind of code:
Public class controlx
public sub new()
dim x as new controlx
End sub
End class
you might think its a silly logical error, but such errors can be very much frustrating and time consuming.always breakdown your code to smaller units so that its manageable as Brian said.
My attempts at writing a simple crawler seem to be confounded by the fact that my target webpage (as would appear in the UI browser control, or through a typical browser application) is not completely accessible as an HTMLDocument (due to frames, javascript, etc.)
The code below executes, and the correct webpage (e.g. the one displaying items 50-59) can even be seen in the control, but where I would expect the “next page” hyperlink retrieved to be “...&start=60”, I see something else – the one corresponding to opening the first catalog page “...&start=10”.
What is odd, is that if I press the button a second time, I DO get what I’m looking for. Even odder to me, if I inserted a MsgBox, say right after I’ve looped to wait until WebBrowserReadyState.Complete, then I get what I’m looking for.
Private Sub ButtonGo_Click(sender As System.Object, e As System.EventArgs) Handles ButtonGo.Click
'start at this URL
'e.g. http://www.somewebsite.com/properties?l=Dallas+TX&co=US&start=50
catalogPageURL = TextBoxInitialURL.Text
WebBrowser1.Navigate(catalogPageURL)
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
'Locate the URL associated with the NEXT>> hyperlink
Dim allLinksInDocument As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
Dim strNextPgLink As String = ""
For Each link As HtmlElement In allLinksInDocument
If link.GetAttribute("className") = "next" Then
strNextPgLink = link.GetAttribute("href")
End If
Next
End Sub
I’ve googled around enough to try things like using a WebBrowser1.DocumentCompleted
event, but that still didn’t work. I’ve tried inserting sleep commands.
I’ve avoided using WebClient and regular expressions, the way I would have ordinarily done this, because I’m convinced using the DOM will be easier for other things I have planned down the road, and I’m aware of HTML Agility Pack but not ambitious enough to learn it. Because it seems there has to be a simple way to have this dang webbrowser.document object synchronized with the stuff you can actually see.
If this is because of javascript, is there a way I can tell the webbrowser to just execute them all?
First question on the forum, looking forward to more (smarter ones hopefully)
Be warned when using webbrowser1.Document or something similar - you will not get 'raw html'
Example: (assume wbMain is a webbrowser control)
RTB_RawHTML.Text = wbMain.DocumentText
Try
RTB_BodyHTML.Text = wbMain.Document.Body.OuterHtml
Catch
debugMessage("Body tag not found.")
End Try
in this example, the code in the body tag as displayed in the body tag portion of RTB_RawHTML will NOT perfectly match the html as displayed in RTB_BodyHTML. Accessing it through (yourwebbrowserhere).Document.Body.OuterHtml appears to 'clean' it somewhat as opposed to the 'raw' html as retreived by (yourwebbrowserhere).DocumentText
This was a problem for me when i was making a web scraper, as it would continually throw me off - sometimes i would try to match a tag and it would find it, and other times it wouldnt even though i was sure it was there. The reason was that i was trying to match the raw html, but i needed to match the 'cleaned' html.
Im not sure if this will help you isolate the problem or not - for me it did.