How to use runJavaScript to simulate an element of a mouse movement page in Python

question:
I saw an answer before because the selenium framework was detected in the previous paragraph, and then I used PyQt5 to make a browser to implement the crawler. I now have the same problem, but the problem is that I don"t know how to use Js statements to simulate mouse movement of an element.

Code:

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
import pyautogui
import time
import sys
app = QApplication([])
view = QWebEngineView()
view.load(QUrl("url"))
view.show()
page = view.page()
-sharpa = 0
-sharpglobal a

def test():
    
    page.runJavaScript("$("-sharpaccount").val(123)")
    page.runJavaScript("$("-sharppassword").val(123)")
    page.runJavaScript("$("-sharpbtn-login").trigger("click")")                   
    time.sleep(1)
-sharp    page.runJavaScript("alert($("-sharpdistance").html())")
    page.runJavaScript("$(".smallImg").trigger("click")")
    
-sharpview.loadFinished.connect(test)
app.exec_()

is that you need to move an element named smallImg in class. The above input and click login are done, and now you are stuck in the mobile part.

specific movement needs:
move the mouse to the coordinates of the smallImg element, click and drag horizontally for a certain distance, and then release. The specific moving distance can not be considered to identify the picture, I can get the distance I need to move.

May.11,2021

I don't know what you mean by "selenium detected", but selenium provides drag-and-drop functionality

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()

refer to python.readthedocs.io/navigating.html-sharpdrag-and-drop" rel=" nofollow noreferrer "> https://selenium-python.readt.

< hr >

as far as I know, currently (August 10, 2018), no javascript can really achieve the drag-and-drop effect.

however, you can refer to the following documentation to generate a drag-and-drop event, and then trigger
https://developer.mozilla.org.

on the specified element.
var event = new DragEvent(type, DragEventInit);

// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);

// Dispatch the event.
elem.dispatchEvent(event);
< hr >

digression: the difficulty of this kind of problem usually does not lie in the beginning or end of the drag-and-drop, and you may need more precise means to control the entire drag-and-drop process, such as generating mouse events outside the browser.


I have also encountered a problem. When using pyqt5 to drive the browser, inject js,. I want to use js to simulate clicking, jumping to the page, and so on. But when I used the same code, runjavascript, I found that these pieces of code were loading all the time, so that my steps could not be completed properly.

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
import time

app = QApplication([])
view = QWebEngineView()
view.load(QUrl("http://www.flyscoot.com/"))
view.show()
page = view.page()

def test1():
    -sharp page.runJavaScript("alert('hello world')")-sharp go
    page.runJavaScript("document.getElementsByClassName('radio-inline')[1].click()")
    page.runJavaScript("document.getElementById('oneway_from').value=' (CAN)'")
    page.runJavaScript("document.getElementById('oneway_to').value=' (SIN)'")
    page.runJavaScript("document.getElementById('oneway_departuredate').value='2018831'")
    page.runJavaScript("document.getElementsByClassName('btn--booking')[1].click()")
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp  -sharp-sharp-sharp-sharp
    page.runJavaScript("document.getElementsByClassName('price--sale')[0].click()")
    page.runJavaScript("document.getElementsByClassName('heading-4')[0].click()")
    page.runJavaScript("document.getElementsByClassName('btn-submit')[0].click()")
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp  -sharp-sharp-sharp-sharp
    page.runJavaScript("document.getElementById('selecttitle1').value='MR'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__First').value='tom'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__Last').value='wang'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__DayOfBirth').value='12'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__MonthOfBirth').value='12'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__YearOfBirth').value='1995'")
    page.runJavaScript("document.getElementById('revPassengersInput_PassengerInfantModels_PassengersInfo_0__Nationality').value='CN'")
    page.runJavaScript("document.getElementsByClassName('radio-inline').click()")
    page.runJavaScript("document.getElementsByClassName('btn-submit').click()")
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp  -sharp-sharp-sharp-sharp
    page.runJavaScript("document.getElementsByClassName('btn-submit').click()")
    page.runJavaScript("document.getElementById('nextFlightButton').click()")
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp  -sharp-sharp-sharp-sharp
    page.runJavaScript("document.getElementsByClassName('btn-submit').click()")
    -sharp-sharp-sharp-sharp-sharp-sharp-sharp-sharp  -sharp-sharp-sharp-sharp
    page.runJavaScript("document.getElementById('revContactInput_WorkPhone_Number').value='xxx'")
    page.runJavaScript("document.getElementById('emailContact').value='xxx'")
    page.runJavaScript("document.getElementsByClassName('form-control').value='xxx'")
    page.runJavaScript("document.getElementsByClassName('radio-inline').click()")
    page.runJavaScript("document.getElementById('revContactInput_ContactViewModel_AddressLine1').value='guojiaqiao'")
    page.runJavaScript("document.getElementById('revContactInput_ContactViewModel_City').value='chengdu'")
    page.runJavaScript("document.getElementById('revContactInput_ContactViewModel_CountryCode').value='CN'")
    page.runJavaScript("document.getElementById('revContactInput_ContactViewModel_ProvinceState').value='FJ'")
    page.runJavaScript("document.getElementById('revContactInput_ContactViewModel_PostalCode').value='401122'")
    page.runJavaScript("document.getElementsByClassName('tab')[2].click()") -sharp Alipay
    page.runJavaScript("document.getElementsByClassName('push-checkbox')[1].click()") -sharp 
    page.runJavaScript("document.getElementsByClassName('btn-submit').click()") -sharp go

view.loadFinished.connect(test1)

app.exec_()
Menu