请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by sanmaodo » 16 Mar 2021, 08:13

想用 SeleniumBasic V3.141 的功能,自动点击这个网页中的“索引”按钮
网页地址:https wyagd001.github.io /zh-cn/docs/AutoHotkey.htm Broken Link for safety
QQ图片20210316204709.png
QQ图片20210316204709.png (16.31 KiB) Viewed 2738 times


我尝试用论坛里找到的办法,但都不成功,希望能得到帮助,先感谢!

Code: Select all

;https://www.autohotkey.com/boards/viewtopic.php?f=7&t=87159

wd := ComObjCreate("SeleniumBasic.IWebDriver")
svc := ComObjCreate("SeleniumBasic.ChromeDriverService")
options := ComObjCreate("SeleniumBasic.ChromeOptions")

svc.CreateDefaultService.driverPath := ".\tools\SeleniumBasic"
svc.HideCommandPromptWindow := True

options.BinaryLocation := "C:\Program Files\Google\Chrome\Application\chrome.exe"
options.DebuggerAddress := "127.0.0.1:9333"

wd.New_ChromeDriver(svc,Options)
wd.ExecuteScript("window.open('https wyagd001.github.io /zh-cn/docs/AutoHotkey.htm')") 
sleep 1000
; wd.FindElementByName("Index tab").Click()  ;无效
; wd.findElementByCssSelector("#head > div > div.h-tabs > ul > li:nth-child(2) > button").Click()  ;无效
; wd.FindElementByXPath("//*[@id='head']/div/div[1]/ul/li[2]/button").Click()  ;无效
wd.Quit()
以上代码来至https://www.autohotkey.com/boards/viewtopic.php?f=7&t=87159 谢谢@burque505
Last edited by sanmaodo on 16 Mar 2021, 13:28, edited 2 times in total.
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: 请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by burque505 » 16 Mar 2021, 12:26

Hello, @sanmaodo. 抱歉,我不会说中文。
我假设您已经注册了SeleniumBasic dll。

Code: Select all

wd := ComObjCreate("SeleniumBasic.IWebDriver")
svc := ComObjCreate("SeleniumBasic.ChromeDriverService")
options := ComObjCreate("SeleniumBasic.ChromeOptions")

svc.CreateDefaultService.driverPath := "C:\Chromium" ; 这必须是正确的“ chromedriver.exe”的路径(仅文件夹,而不是可执行文件)。
svc.HideCommandPromptWindow := True

options.BinaryLocation := "C:\Chromium\chrome-win\chrome.exe" ; 这必须是具有正确版本的“ chrome.exe”可执行文件的路径。
options.AddArgument("--start-maximized")
;options.DebuggerAddress := "127.0.0.1:9333" ; 尝试不使用调试器地址

wd.New_ChromeDriver(svc,Options)
wd.ExecuteScript("window.open('https://wyagd001.github.io/zh-cn/docs/AutoHotkey.htm')") 
sleep 1000
;wd.FindElementByName("Index tab").Click()  ;无效
;wd.findElementByCssSelector("#head > div > div.h-tabs > ul > li:nth-child(2) > button").Click()  ;无效
;wd.FindElementByXPath("//*[@id='head']/div/div[1]/ul/li[2]/button").Click()  ;无效
sleep 5000
wd.Quit()
刚尝试时,此代码对我有用。希望您一切顺利!
sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: 请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by sanmaodo » 16 Mar 2021, 16:58

Hello, @burque505,非常感谢您的回复,抱歉,之前我没有把问题描述清楚。

我的需求是这样的:
1,在现有的浏览器窗口中...
2,在新标签页中...
3,打开网址并执行自动点击。

我找到了一个办法,但并不是很稳定,在测试过程中偶尔会出现问题,期待您对它进行改进。
首先,现有的浏览器窗口必须要添加参数打开:

Code: Select all

Run, Chrome.exe "--remote-debugging-port=9333"
然后,

Code: Select all

;https://www.autohotkey.com/boards/viewtopic.php?f=7&t=87159
SetBatchLines, -1

wd := ComObjCreate("SeleniumBasic.IWebDriver")
svc := ComObjCreate("SeleniumBasic.ChromeDriverService")
options := ComObjCreate("SeleniumBasic.ChromeOptions")

svc.CreateDefaultService.driverPath := ".\tools\SeleniumBasic"
svc.HideCommandPromptWindow := True

options.BinaryLocation := "C:\Program Files\Google\Chrome\Application\chrome.exe"
options.DebuggerAddress := "127.0.0.1:9333"

wd.New_ChromeDriver(svc,Options)

if WinExist("ahk_class Chrome_WidgetWin_1")
WinActivate

wd.ExecuteScript("window.open('https wyagd001.github.io /zh-cn/docs/AutoHotkey.htm')")  Broken Link for safety 
tabs :=wd.WindowHandles
n := tabs.MaxIndex()
wd.SwitchTo.Window(tabs[n])

wd.FindElementByXPath("//*[@id='head']/div/div[1]/ul/li[2]/button").click()
wd.Quit()
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: 请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by burque505 » 16 Mar 2021, 17:48

@sanmaodo, 到目前为止,您的代码对我来说还不错。也许试试这个:

Code: Select all

wd.ExecuteScript("document.querySelector(""button[title='快捷键: ALT+N']"").click();")
代替

Code: Select all

wd.FindElementByXPath("//*[@id='head']/div/div[1]/ul/li[2]/button").click()
sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: 请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by sanmaodo » 16 Mar 2021, 17:58

@burque505 效果很好,谢谢!!!
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: 请问如何用 SeleniumBasic 自动点击网页中的某个按钮?

Post by burque505 » 17 Mar 2021, 07:30

@sanmaodo, 我很高兴它有效。感谢您的客气话。
Post Reply

Return to “请求帮助”