VBA Selenium FindElementByXPath找不到元素 [英] VBA Selenium FindElementByXPath doesnt find element
问题描述
代码示例和结果1:
错误激活
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Gethttps://www.thesupplementstore.co.uk/品牌/ best_nutrition?page = 4
关于错误简历Next
对于每个帖子在driver.FindElementsByClass(desc)
单元格(i,1)= post.FindElementByTag(a ).Attribute(title)
单元格(i,2)= Trim(Split(post.FindElementByClass(size)。Text,:)(1))
单元格(i, 3)= post.FindElementByXPath(.// span [@ class ='now'] // span [@ class ='pricetype-purchase-unit multi-price'] // span [@ class ='blu-price blu -price-initialised'])。文本
单元格(i,4)= post.FindElementByTag(a)。属性(hr ef)
i = i + 1
下一个帖子
End Sub
代码示例和结果2:关闭错误
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Gethttps://www.thesupplementstore.co.uk/brands/optimum_nutrition?page=4
'在错误恢复下一步
对于每个帖子在驱动程序$($)
单元格(i,1)= post.FindElementByTag(a)属性(title)
单元格(i,2)= Trim(Split(post。 FindElementByClass(size)。Text,:)(1))
单元格(i,3)= post.FindElementByXPath(.// span [@ class ='now'] // span [@ class ='pricetype-purchase-unit multi-price'] // span [@ class ='blu-price blu-price-ini文本
单元格(i,4)= post.FindElementByTag(a)。属性(href)
i = i + 1
下一篇文章
End Sub
代码示例和结果3:错误激活
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Gethttps://www.thesupplementstore.co.uk/brands/optimum_nutrition
On Error Resume Next
对于每个帖子在driver.FindElementsByClass(desc)
单元格(i,1)= post.FindElementByTag(a)。属性(title)
cell(i,2)= Trim(Split(post.FindElementByClass(size)。Text,:)(1))
单元格(i,3)= post.FindElementByXPath(跨度[@类= '现在'] //跨度[@类='pricetype-珀切斯文本
单元格(i,4)= post.FindElementByTag(a)。属性(href)
i = i + 1
下一个帖子
End Sub
第一个例子返回网站除了价格之外的所有74个项目,很长一段时间大约两分钟。
第二个例子只将标题返回到工作表的第一个单元格,并弹出错误。
第三个例子只返回21,但是错过了那些没有标签的项目的返回价格。脚本运行速度非常快,10秒以内。
请咨询关于如何将所有74个项目与标题,大小,价格,href一起返回。
您正在处理的页面已应用了铺设方法。这是因为所有项目一次都不加载;相反,当您向下滚动时,它会加载其余的。我在代码中使用了一个小的javascript函数,它解决了这个问题。我希望这是你正在寻找的结果。
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
driver.Gethttps://www.thesupplementstore.co.uk/brands/optimum_nutrition
关于错误简历Next
Do While EndofPage = False
PrevPageHeight = CurrentPageHeight
CurrentPageHeight = driver.ExecuteScript(window.scrollTo(0,document.body.scrollHeight); var CurrentPageHeight = document.body.scrollHeight; return CurrentPageHeight; )
driver.Wait 3000
如果PrevPageHeight = CurrentPageHeight然后
EndofPage = True
结束如果
循环
对于每个帖子在驱动程序。 FindElementsByXPath(// li [contains(@ class,'prod')])
i = i + 1:Cells(i,1)= post.FindElementByXPath(.// a)Attribute标题)
单元格(i,2)=拆分(post.FindElementByXPath(.// p [@ class ='size'])。文本,:) (1)
单元格(i,3)= post.FindElementByXPath(.// p [@ class ='price'] // span [@ class ='now'] // span | .// p [@ class ='price'] // span [@ class ='dynamictype-single'])。文本
单元格(i,4)= post.FindElementByXPath(.// a)。属性href)
下一篇文章
End Sub
I have written a VBA which opens a web link using selenium chrome web driver to scrape of data and I got several problems with which I need your advice on guys.
Code example and outcome 1: On error actived
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition?page=4"
On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
Code example and outcome 2: On error deactivated
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition?page=4"
'On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
Code example and outcome 3: On error activated
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
i = 1
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition"
On Error Resume Next
For Each post In driver.FindElementsByClass("desc")
Cells(i, 1) = post.FindElementByTag("a").Attribute("title")
Cells(i, 2) = Trim(Split(post.FindElementByClass("size").Text, ":")(1))
Cells(i, 3) = post.FindElementByXPath(".//span[@class='now']//span[@class='pricetype-purchase-unit multi-price']//span[@class='blu-price blu-price-initialised']").Text
Cells(i, 4) = post.FindElementByTag("a").Attribute("href")
i = i + 1
Next post
End Sub
First example returns all of the 74 items from the website apart from price but in a very long period of time about two minutes.
Second example only returns title into the first cell of the sheet and pops the error out.
Third example returns only 21 but misses to return price of those items that do not have now label. Script runs very quickly, under 10 seconds.
Please advice on how to return all 74 items back together with title, size, price, href.
The page you are dealing with has got lay-loading method applied. This is because all items don't load at a time; rather, it loads the rest when you scroll downmost. I used a small javascript function within the code and it solves the issue. I hope this is what the result you were looking for.
Sub test_supplements_store()
Dim driver As New ChromeDriver
Dim post As Object
driver.Get "https://www.thesupplementstore.co.uk/brands/optimum_nutrition"
On Error Resume Next
Do While EndofPage = False
PrevPageHeight = CurrentPageHeight
CurrentPageHeight = driver.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);var CurrentPageHeight=document.body.scrollHeight;return CurrentPageHeight;")
driver.Wait 3000
If PrevPageHeight = CurrentPageHeight Then
EndofPage = True
End If
Loop
For Each post In driver.FindElementsByXPath("//li[contains(@class,'prod')]")
i = i + 1: Cells(i, 1) = post.FindElementByXPath(".//a").Attribute("title")
Cells(i, 2) = Split(post.FindElementByXPath(".//p[@class='size']").Text, ": ")(1)
Cells(i, 3) = post.FindElementByXPath(".//p[@class='price']//span[@class='now']//span|.//p[@class='price']//span[@class='dynamictype-single']").Text
Cells(i, 4) = post.FindElementByXPath(".//a").Attribute("href")
Next post
End Sub
这篇关于VBA Selenium FindElementByXPath找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!