Selenium 从 WebElement Python 获取文本
Selenium getting text from WebElement Python
您好,我想要从我的尺码网络元素中打印数据
这是网站 HTML
<div class="size-option"><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="163" data-quantity="4">
<span class="checker-box">8</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="164" data-quantity="5">
<span class="checker-box">8.5</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="165" data-quantity="3">
<span class="checker-box">9</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="167" data-quantity="8">
<span class="checker-box">10</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="168" data-quantity="5">
<span class="checker-box">10.5</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="169" data-quantity="3">
<span class="checker-box">11</span>
</label></div>
print(size.text)
给我
AttributeError: 'list' object has no attribute 'text'
这是我的元素
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
和
for value in size:
print(value.text)
不起作用
print(size[0].text)
只要给我空的,但我要打印列表中的跨度数字。
由于“size”是一个网络元素列表,如果您希望打印其所有元素的文本值,则必须遍历列表并打印每个元素文本,如下所示:
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
for el in size:
print(el.text)
否则,如果 size 打算成为单个元素,我不知道 "//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div"
XPath 定位是什么 - 如果是这样,您应该使用
size=driver.find_element_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
而不是
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
您好,我想要从我的尺码网络元素中打印数据
这是网站 HTML
<div class="size-option"><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="163" data-quantity="4">
<span class="checker-box">8</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="164" data-quantity="5">
<span class="checker-box">8.5</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="165" data-quantity="3">
<span class="checker-box">9</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="167" data-quantity="8">
<span class="checker-box">10</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="168" data-quantity="5">
<span class="checker-box">10.5</span>
</label><label class="custom-input type-box size-box">
<input class="form-control" type="radio" name="sizeChartId" value="169" data-quantity="3">
<span class="checker-box">11</span>
</label></div>
print(size.text)
给我
AttributeError: 'list' object has no attribute 'text'
这是我的元素
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
和
for value in size:
print(value.text)
不起作用
print(size[0].text)
只要给我空的,但我要打印列表中的跨度数字。
由于“size”是一个网络元素列表,如果您希望打印其所有元素的文本值,则必须遍历列表并打印每个元素文本,如下所示:
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
for el in size:
print(el.text)
否则,如果 size 打算成为单个元素,我不知道 "//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div"
XPath 定位是什么 - 如果是这样,您应该使用
size=driver.find_element_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")
而不是
size=driver.find_elements_by_xpath("//*[@id='product']/div[1]/div/div[1]/div[2]/div/div[3]/div")