+ Reply to Thread
Results 1 to 14 of 14

Move webelement outer screen

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Move webelement outer screen

    Hello
    I use VBA with Seleniumbasic on my Windows 10 system.
    I log in to a website where I have an account and surf to a page where my personal data is stored, which can have many web elements.
    To sort that data, I created a piece of code that puts the web elements in the correct order. This code works correctly but only if the element needs to be moved within the screen. For example, if a web element has to move 30 places, it doesn't work because this element is off screen. The web page scrolls down but does not move the web element.
    As long as X1 < 14 then it works, element 15 is at the bottom, just out of view
    Can anyone help me with this?

    Sub TestVidsVerplaatsen()                                                                                                
    Dim TelBy As Integer
    Dim by As New selenium.by
    Dim elePak As WebElement, elePlak As WebElement
    Dim bot As New selenium.ChromeDriver
    Dim X As Integer, X1 as Integer
    
    With bot
        '.AddArgument ("headless")                                                                                         
        .AddArgument "window-size=1920,1080"
        Url = MyURL                                                                     
        .Get Url
        DoEvents
        
         .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[1]/div/div/input").SendKeys (MyMail)
        .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[2]/div/div/input").SendKeys (MyPass)
        .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[3]/div/button").submit
                                                                                                                            
        TelBy = 0
        Do
           TelBy = TelBy + 1
           .Wait (20)
           DoEvents
        Loop Until TelBy = 100 Or .IsElementPresent(by.XPath("/html/body/app-root/app-dashboard/div[2]/app-breadcrumb/div/div/div/div/div"))
                                                                                                                           
        .Get URLmyDATA
        
        X = 2     'Example: x = position of the element to be moved
        X1 = 8   'Example: x1 = position to move it to
        
        
        Set elePak = .FindElementByXPath("/html/body/div[5]/div[3]/div/div/ul/li[" & X & "]")
        Set elePlak = .FindElementByXPath("/html/body/div[5]/div[3]/div/div/ul/li[" & X1 & "]")
        
        .actions.DragAndDrop(elePak, elePlak).ClickAndHold.Perform
        .actions.Release(elePak).Perform
        DoEvents
        .FindElementByXPath("/html/body/div[5]/div[3]/div/button").Click              'confirm
        .Quit
    End With
    End Sub

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    You should be able to first set element by xpath.
    Then use...
    driver.execute_script("arguments[0].scrollIntoView(true);", element)
    to get the element into view.
    For sanity check, you can then check that your element is visible by using... WebDriverWait.
    Ex; Wait until element is visible. Or 10 sec timeout.
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[5]/div[3]/div/div/ul/li[" & X1 & "]")))
    Edit: In general you should maximize window before driver.execute_script to get the element into view using driver.maximize_window()
    Last edited by CK76; 05-27-2024 at 01:16 PM.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Thank you CK76 for your help, unfortunately, that did not provide the solution.
    As long as the movement is within the screen (X1 < 15), everything goes well. If it falls outside the screen, the screen will scroll to the correct position but the movement will not take place.
    This is my full code, Would you mind taking a look at what's going wrong?


    
    
    Sub TestVidsVerplaatsen()                                                                                               
    'sort videos into Kinomap playlist
    
    Dim Telby As Integer, X3 As Integer
    Dim by As New Selenium.by
    Dim elePak As WebElement, elePlak As WebElement
    Dim bot As New ChromeDriver
    
    With bot
        '.AddArgument ("headless")                                                                                           
        'this hides the Chrome window when it is not inhibited
        .AddArgument "window-size=600,800"
        'When logging in, the chrome screen is small
        
        url = login page                                                                      
        'login page
        .Get url
        
        Telby = 0
        Do
           Telby = Telby + 1
           .Wait (20)
           DoEvents
        Loop Until Telby = 100 Or .IsElementPresent(by.xpath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[1]/div/div/input"))
        'wait until input field is present   
     
        If Telby = 100 Then got Fout
        
        .FindElementByXpath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[1]/div/div/input").SendKeys (MyMail)
        .FindElementByXpath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[2]/div/div/input").SendKeys (MyPass)
        .FindElementByXpath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[3]/div/button").Submit
         'now you are logged in
    
        Telby = 0
        Do
           Telby = Telby + 1
           .Wait (20)
           DoEvents
        Loop Until Telby = 100 Or .IsElementPresent(by.xpath("/html/body/app-root/app-dashboard/div[2]/app-breadcrumb/div/div/div/div/div"))
        'the Dashboard button is now present, but we do NOT click on it.  only to wait for the page to load
     
        .Window.maximize                                                                                                    
        'make Chrome full screen
         
        S = URL & Number Playlist
        .Get S                                                                                                              
        'tested using msgbox and now element for eleplak is already present, even without ScrollIntoView
    
        Do Until InStr(1, .url, Right(S, 5)) > 0                                                                            
            'Here we cannot check for web element so check for the URL
           .Wait (100)
           DoEvents
        Loop
        
        On Error GoTo Fout
        .FindElementByXpath("/html/body/div[2]/div/a[2]").Click                                                             
        'accept cookies
        
        X = 2                                                                                                              
        'location of source elepak element
    
        X1 = 12                                                                                                             
        'location of destination eleplak element
        
        .FindElementByXpath("/html/body/div[5]/div[3]/div/div/ul/li[" & X & "]").ScrollIntoView                             
        'put elepak at the bottom of the screen because it is FALSE, if TRUE it is at the top of the screen
        Set elePak = .FindElementByXpath("/html/body/div[5]/div[3]/div/div/ul/li[" & X & "]")                               
        'put element to be moved in elePak
        .Wait (100)
        
        .FindElementByXpath("/html/body/div[5]/div[3]/div/div/ul/li[" & X1 & "]").ScrollIntoView                             
        Set elePlak = .FindElementByXpath("/html/body/div[5]/div[3]/div/div/ul/li[" & X1 & "]")                             
        'put the position where elepak should go in eleplak
        .Wait (100)
        
        .actions.DragAndDrop(elePak, elePlak).Perform
        'the page scrolls to the correct position but does not move when elePlak falls outside the screen, even using ScrollIntoView True or ScrollIntoView False
        DoEvents
        
        .FindElementByXpath("/html/body/div[5]/div[3]/div/button").Click                                                    
        'Confirm new order
    
    MsgBox Left(elePak.Text, 30) & " = moved, just below " & vbLf & Left(elePlak.Text, 30)
        'check if element has been moved
    
    Einde:
        .Quit
        'close Chrome
    
    End With
    Exit Sub
    
    Fout:
    bot.Quit
    Err.Clear
    MsgBox "An error has occurred   " & Err.Number
    End Sub

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    In that case, try using offset. Instead of element drag and drop, use offset drag and drop. First you should find coordinate of the destination element.

    Ex: x is horizontal offset, y is vertical offset.
    originH = elePak.getLocation().getX()
    originV = elePak.getLocation().getY()
    destH = elePlak.getLocation().getX()
    destV = elePlak.getLocation().getY()
    
    xOffset = (destH - originH)  'You may want to add small value to this to offset from edge for x & y
    yOffset = (destV - originV)
    .actions.dragAndDropBy(elePak, xOffset, yOffset).perform
    If above still does not work. Instead of using built-in drag and drop... you will have to use action builder / action chains to build scroll operation (move_to) along with click and hold etc.

  5. #5
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Thank you again for your response.
    I have already tested with offset in recent weeks, but without results. But I will try this again.
    Then I will use the 'build' option, although that is a different programming technique than VBA with SeleniumBasic

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    You should have ActionChains, assuming you are using GCuser99's SeleniumVBA

  7. #7
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Thank you, I will Google to learn this.
    It may take a while, but when it works I will post the code here so that others can benefit from it as well.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    Good luck. Unfortunately I'm not aware of any site that does drag and drop with scroll. So can't write a code to test.
    If you have public site that has this, I could help a bit better.

  9. #9
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Good morning CK76, thank you again for your help, it is much appreciated.
    I don't just ask for help, I only do that after trying for weeks without results. The site where I have an account offers a 14-day trial period to test, after which a subscription is required. If you agree, I can create an account with a playlist in it. I will then put 20 training videos in that playlist and I will send you the code as I have it now, in which the URLs are visible and the login name and password of the test account are included, by PM. Put that code in a normal module. The results achieved will be posted publicly on this forum. Let me know what you think of this proposal. Perhaps it is possible to send you a small Excel workbook with just that code.

    After your previous message, I have already been busy and installed SeleniumWrapper on my system and added this Library Reference to the workbook.

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    Sure. I won't have time to look at it much this week. But perhaps next week.

  11. #11
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Thank you, I will send the code with the login details next Tuesday or Wednesday. Have a nice weekend.

  12. #12
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    I finally got it working with VBA and SeliniumBasic.

    
    Sub test_drag_and_drop()
        Dim bot As New Selenium.ChromeDriver, By As New Selenium.By
        Dim elePak As Selenium.WebElement, elePlak As Selenium.WebElement
        Dim X As Integer, X1 As Integer, X2 As Integer, Ronde As Integer, str As String
        
        With bot
           .AddArgument "window-size=600, 800"
        
           .Get MyLogin-URL
           .Wait 500
        
           .FindElementByXPath("/html/body/app-root/app-cookie/div/div/div[2]/button[2]").Click
        
           .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[1]/div/div/input").SendKeys (MyMail)
           .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[2]/div/div/input").SendKeys (MyPass)
           .FindElementByXPath("/html/body/app-root/app-marketing/app-login/div/div/div/form/section/div[3]/div/button").Submit
           .Wait (1500)
        
           .Get URL of the playlist
           .Window.Maximize
           .Wait (1500)
        
           X = 2                        'Position of the element to be moved
           X1 = 48                      'Position of the 'where to' element
           Set elePlak = .FindElementByXPath("/html/body/div[5]/div[3]/div/div/ul/li[" & Ronde + 1 & "]")
           str = elePlak.Text           'Save the title of the 'where to' element
           
           For Ronde = X To X1          'Move the element one position at a time
              X2 = Ronde
              
              Set elePak = .FindElementByXPath("/html/body/div[5]/div[3]/div/div/ul/li[" & Ronde & "]")
              If .IsElementPresent(By.XPath("/html/body/div[5]/div[3]/div/div/ul/li[" & (Ronde + 1) & "]")) Then
                                        'Because when moving to the last position element (Ronde + 1) does not exist
                 Set elePlak = .FindElementByXPath("/html/body/div[5]/div[3]/div/div/ul/li[" & (Ronde + 1) & "]")
                 Call ScrollIntoViewCenter(elePlak)  'This is a sub, found on the internet, that always places the element elePlak in the center of the screen, not at the top or bottom
                 .actions.DragAndDrop(elePak, elePlak).Perform
                 .Wait (200)
                 DoEvents
                 If str = elePlak.Text Then Exit For  'Now the text of elePlak is the same as the position we were originally looking for
              End If
           Next Ronde
           .FindElementByXPath("/html/body/div[5]/div[3]/div/button").Click  'Confirm new order
           .Wait (200)
           DoEvents
    Einde:
           .Quit
        End With
    End Sub

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,950

    Re: Move webelement outer screen

    To add to OP. What was causing issue here, is that site did not allow scroll operation while ClickAndHold() is taking place. Thus limiting drag and drop to on screen.

    Op found module that drops item in centre of screen to do it in multiple steps.

  14. #14
    Registered User
    Join Date
    07-24-2023
    Location
    Oirschot, Netherlands
    MS-Off Ver
    2016
    Posts
    11

    Re: Move webelement outer screen

    Hello CK76
    At the moment I am working on putting this code in a do...loop. Multiple videos may need to be sorted. These movements will mainly be moved downwards, but upwards should also be possible. Not simple because after moving element(2) to position 8, the element(3) is then in the second position. But I know how I'm going to do this and it already works pretty well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Buttons to move in relation to userform on screen
    By JamesT1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2013, 05:10 AM
  2. Using VBA to move screen selection
    By MDubbelboer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-23-2006, 05:50 PM
  3. Select a Cell - Move Row to the Top of the screen
    By CRayF in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-26-2005, 06:05 AM
  4. Move the position of spreadsheet on the screen
    By Souris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2005, 01:05 AM
  5. Replies: 6
    Last Post: 05-29-2005, 12:35 AM
  6. [SOLVED] arrow keys move screen but not curser
    By JReiss in forum Excel General
    Replies: 1
    Last Post: 04-21-2005, 11:06 AM
  7. Replies: 3
    Last Post: 02-25-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1