問(wèn)題:
之前看到一個(gè)回答是因?yàn)閟elenium框架被前段檢測(cè)了,然后用PyQt5制作瀏覽器去實(shí)現(xiàn)爬蟲,我現(xiàn)在也遇到了同樣的問(wèn)題,但是問(wèn)題是在我不知道如何用Js的語(yǔ)句去模擬鼠標(biāo)移動(dòng)某個(gè)元素。
代碼:
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()
#a = 0
#global a
def test():
page.runJavaScript("$('#account').val(123)")
page.runJavaScript("$('#password').val(123)")
page.runJavaScript("$('#btn-login').trigger('click')")
time.sleep(1)
# page.runJavaScript("alert($('#distance').html())")
page.runJavaScript("$('.smallImg').trigger('click')")
#view.loadFinished.connect(test)
app.exec_()
就是需要移動(dòng)一個(gè)class名為smallImg的元素, 上面的輸入和點(diǎn)擊登錄都完成了,現(xiàn)在卡在移動(dòng)那塊。
具體的移動(dòng)需要:
鼠標(biāo)移動(dòng)到smallImg這個(gè)元素的坐標(biāo),點(diǎn)擊并水平拖動(dòng)一定的距離,然后釋放。具體的移動(dòng)距離可以先不考慮識(shí)別圖片,我可以獲取到需要移動(dòng)的距離。
不知道你所說(shuō)的 "selenium 被檢測(cè)到" 是什么情況,不過(guò) selenium 提供了拖放功能
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()
參考 https://selenium-python.readt...
據(jù)我所知,目前(2018年8月10日)還沒(méi)有 javascript 能實(shí)現(xiàn)真正可用的拖放效果。
不過(guò)你可以參考下面的文檔,生成拖放事件,然后在指定元素上觸發(fā)試試
https://developer.mozilla.org...
var event = new DragEvent(type, DragEventInit);
// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
題外話:這類問(wèn)題的難點(diǎn)通常不在拖放的起點(diǎn)或終點(diǎn),你可能需要更精確的手段去控制整個(gè)拖放過(guò)程,比如在瀏覽器外生成鼠標(biāo)事件。
我現(xiàn)在也遇到了一個(gè)問(wèn)題,同求解答,利用pyqt5驅(qū)動(dòng)瀏覽器的時(shí)候,注入js,我想全程使用js模擬點(diǎn)擊,跳轉(zhuǎn)頁(yè)面等。但是當(dāng)我使用相同的代碼,runjavascript的時(shí)候,發(fā)現(xiàn)這幾段代碼一直在加載,導(dǎo)致我的步驟無(wú)法正常完成。
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():
# page.runJavaScript("alert('hello world')")# 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='2018年8月31日'")
page.runJavaScript("document.getElementsByClassName('btn--booking')[1].click()")
############################ 分割線 ##################################
page.runJavaScript("document.getElementsByClassName('price--sale')[0].click()")
page.runJavaScript("document.getElementsByClassName('heading-4')[0].click()")
page.runJavaScript("document.getElementsByClassName('btn-submit')[0].click()")
############################ 分割線 ##################################
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()")
############################ 分割線 ##################################
page.runJavaScript("document.getElementsByClassName('btn-submit').click()")
page.runJavaScript("document.getElementById('nextFlightButton').click()")
############################ 分割線 ##################################
page.runJavaScript("document.getElementsByClassName('btn-submit').click()")
############################ 分割線 ##################################
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()") # Alipay
page.runJavaScript("document.getElementsByClassName('push-checkbox')[1].click()") # 接受條約
page.runJavaScript("document.getElementsByClassName('btn-submit').click()") # go
view.loadFinished.connect(test1)
app.exec_()
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。