问题描述
我要使用硒来迭代gog.com顶部轮播中的游戏我想打印所有价格,下面是轮播中我想要的信息的2个随机xpaths
using selenium i want to iterate through the games in the carousel at the top of gog.comi want to print all the prices and below are 2 random xpaths to the information i want in the carousel
/html/body/div[2]/div/div[3]/div/div[3]/div[2]/div/a[4]/div[2]/div/div/div[3]/div/span/span
/html/body/div[2]/div/div[3]/div/div[3]/div[2]/div/a[10]/div[2]/div/div/div[3]/div/span/span
这使我想遍历第一个元素.每个都是不同的游戏.但它要么找不到该路径,要么不返回任何数据,要么返回一个没有属性.text或.innerhtml的对象,这样我就无法获取我想要的信息
this makes me want to iterate through the first a element. each a being a different game. but it either can not find that path, returns no data or returns an object with no attribute .text or .innerhtml such that i cant get the info i want
下面是我的脚本
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"../Downloads/chromedriver.exe")
driver.get('https://www.gog.com/')
driver.maximize_window()
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"/html/body/div[2]/div/div[3]/div/div[3]/div[2]/div/a[10]/div[2]/div/div/div[3]/div/span/span")))
for i in range(15):
i+=1;
A = "/html/body/div[2]/div/div[3]/div/div[3]/div[2]/div/a["+str(i)+"]/div[2]/div/div/div[3]/div/span[2]/span"
try:
print(driver.find_elements_by_xpath(A));
print(driver.find_elements_by_xpath(A).text);
print(str(driver.find_elements_by_xpath(A)));
finally:
pass
下面是它返回的对象的一个示例
below is an example of the object it returns
[<selenium.webdriver.remote.webelement.WebElement (session="3ec9e534c096a49d3110dea89fea3864", element="0bf0c6b6-fb35-4e26-8bf4-acbb7431eafb")>]
下面是它返回的错误的一个示例
below is an example of the error it returns
Traceback (most recent call last):
File "C:\Users\User\Desktop\mi faaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak.py", line 16, in <module>
print(driver.find_elements_by_xpath(A).text);
AttributeError: 'list' object has no attribute 'text'
下面是我想要从中获取信息的实际元素的示例
below is an example of the actual element i want the information from
< span class ="big-spot__price-amount" ng-bind ="tile.data.price.finalAmount"> 13.69</span>
如何使它在此页面顶部的轮播中打印所有价格 https://www.gog.com/
how can i make it print all the prices in the carousel at the top of this page https://www.gog.com/
推荐答案
您正在将 find_elements_by_xpath 方法用于单个元素的xpath,这将返回一个列表.因此,请改用 find_element_by_xpath .
You are using find_elements_by_xpath method for a xpath for single element, which is returning a list. So instead use find_element_by_xpath.
建议:不要编写绝对的xpath,而应使其通用.使您的代码如下所示:
Suggestion: Instead of writing absolute xpath, make it generic. Make your code look like this:
carousel_xpath = '//your generic xpath here'
carousel_elements = driver.find_elements_by_xpath(carousel_xpath)
for carousel in carousel_elements:
print(carousel.text)
//do your things here
这篇关于使用python遍历硒中的轮播对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!