‘7’ 运行时错误 – 没有找到元素错误 – 未找到元素

huangapple go评论89阅读模式
英文:

'7' Runtime Error-NoSuchElementError-Element not found

问题

I can help you translate the non-code part of your text. Here's the translation:

"能帮我了解我错过了什么吗?
使用Selenium驱动程序,我想通过网站boulanger.com上的搜索功能,通过sendkeys,点击搜索按钮,自动收集特定产品的价格。

在执行VBA代码时,我遇到了这个错误。

'7' 运行时错误 - NoSuchElementError - 未找到元素

搜索框的Xpath如下;

//*[@id="search__component"]//div[1]/div/form/input
/html/body/header/div/div/div[3]/section/bl-search//div[1]/div/form/input

我想做的是通过.Sendkey modelcode将型号代码放到服务器上并获取搜索结果,但卡在找到正确元素上。

(https://www.boulanger.com/)

尝试使用FineElementbyCSSFindElementbyXPath但仍然不起作用"

Please note that I've retained the code-related parts in their original language, as requested. If you have any further translation needs or questions, feel free to ask.

英文:

Would you help me to know what I am missing?
Using Selenum driver, I wnat to automate collecting prices of specific products by sendkeys, search button click via the search fuction on the website - boulanger.com.

sub BLG()
...

    url = "https://www.boulanger.com/"
    driver.Start "Chrome"
    driver.Get url

    For i = 1 To ModelCnt
        modelcode = LCase(Cells(4 + i, 3).Value)
        driver.FindElementByXPath("//*[@id=""search__component""]//div[1]/div/form/input").SendKeys modelcode
        driver.FindElementByXPath("//*[@id=""search__component""]//div[1]/div/form/button").Click
...
    Next i

end sub

when executing the VBA codes, I have this error.

'7' Runtime Error-NoSuchElementError-Element not found

The Xpath of search box is as below;

//*[@id="search__component"]//div[1]/div/form/input
/html/body/header/div/div/div[3]/section/bl-search//div[1]/div/form/input

What I want to do is put the model code by .Sendkey modelcode to the server and get search result but sticked at finding the correct element.

(https://www.boulanger.com/)

Tried to use FineElementbyCSS, FindElementbyXPath but still not working

答案1

得分: 0

这段VBA代码对我来说有效。我不得不一步一步地构建它,点击搜索框是最难搞清楚的部分。

Option Explicit

Sub sbBLG()
    Dim chr As ChromeDriver
    Set chr = New ChromeDriver
    Dim we As WebElement
    Dim sURL As String
    sURL = "https://www.boulanger.com/"
    Dim sModelCode As String
    sModelCode = "HT-A7000"
    chr.Start "Chrome"
    chr.Get sURL
    chr.Window.Maximize
    sbDelay (100000)
    chr.SwitchToFrame (0)
    Set we = chr.FindElementByXPath("/html/body")
    we.FindElementById("btnAll-on").Click
    chr.SwitchToParentFrame
    Set we = chr.FindElementByXPath("/html/body")
    sbDelay (100000)
    we.FindElementById("search__component").Click
    we.FindElementById("search__component").SendKeys sModelCode
    Dim Keys As Selenium.Keys
    Set Keys = New Selenium.Keys
    we.FindElementById("search__component").SendKeys Keys.Enter
    sbDelay (100000)
    Set we = chr.FindElementByXPath("/html/body")
    MsgBox we.FindElementByClass("price__amount").Attribute("outerHTML")
    MsgBox we.FindElementByClass("price__amount").Text
    chr.Quit
End Sub
Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay: DoEvents: Next i: End Sub
英文:

This VBA code works for me. I had to build it up step by step, the click in the search box was the hardest to figure out.

Option Explicit

Sub sbBLG()
    Dim chr As ChromeDriver
    Set chr = New ChromeDriver
    Dim we As WebElement
    Dim sURL As String
    sURL = "https://www.boulanger.com/"
    Dim sModelCode As String
    sModelCode = "HT-A7000"
    chr.Start "Chrome"
    chr.Get sURL
    chr.Window.Maximize
    sbDelay (100000)
    chr.SwitchToFrame (0)
    Set we = chr.FindElementByXPath("/html/body")
    we.FindElementById("btnAll-on").Click
    chr.SwitchToParentFrame
    Set we = chr.FindElementByXPath("/html/body")
    sbDelay (100000)
    we.FindElementById("search__component").Click
    we.FindElementById("search__component").SendKeys sModelCode
    Dim Keys As Selenium.Keys
    Set Keys = New Selenium.Keys
    we.FindElementById("search__component").SendKeys Keys.Enter
    sbDelay (100000)
    Set we = chr.FindElementByXPath("/html/body")
    MsgBox we.FindElementByClass("price__amount").Attribute("outerHTML")
    MsgBox we.FindElementByClass("price__amount").Text
    chr.Quit
End Sub
Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay:  DoEvents: Next i: End Sub

huangapple
  • 本文由 发表于 2023年5月18日 13:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定