问题描述
这是我正在尝试使用Python Selenium进行网页抓取的 html
代码段.
This is the html
snippet I am trying to web scrape using Python Selenium.
我正在尝试获取 span数据绑定
内的文本 Add to bag
.
I am trying to get the text Add to bag
which is inside a span data-bind
.
<div class="is-add-item-saving" data-bind="visible: isBusy" style="display: none;"></div>
<span class="aria-live" aria-role="status" aria-live="polite" data-bind="{ text: ariaLiveText }"></span>
<button data-bind="click: addToBag, css : buttonCss, attr: { 'aria-label': resources.pdp_cta_add_to_bag, disabled: isBusy }, markAndMeasure: 'pdp:add_to_bag_interactive'" data-test-id="add-button" aria-label="Add to bag">
<span class="product-tick" data-bind="visible: showProductTick" style="display: none;"></span>
<span data-bind="text: buttonText">Add to bag</span>
</button>
这是我到目前为止尝试过的.
This is what I have tried so far.
instock_element = driver.find_elements_by_xpath("//span[contains(@data-bind,'text: buttonText')]")
instock_element = driver.find_elements_by_xpath("//*[contains(text(), 'Add to bag')]")
当我遍历这些 instock_elements
时,
for value in instock_element:
print("text : ",value.text)
print(" id : ",value.id)
if len(value.text) == 0:
text = value.id
else:
print(value.text)
text = value.text
ins_list.append(text)
这些给了我一些随机值,例如 6489355d-9dd3-4d77-a0d7-b134ce48fae7
,但没有文本 Add to bag
.
These are giving me random values like 6489355d-9dd3-4d77-a0d7-b134ce48fae7
but not the text Add to bag
.
推荐答案
要打印文本 添加到购物袋
,您可以使用以下:
To print the text Add to bag
you can use either of the following Locator Strategies:
-
使用
css_selector
和get_attribute("innerHTML")
:
print(driver.find_element_by_css_selector("button[data-test-id='add-button'][aria-label='Add to bag'] span").get_attribute("innerHTML"))
使用 xpath
和 text 属性:
print(driver.find_element_by_xpath("//button[@data-test-id='add-button' and @aria-label='Add to bag']//span").text)
理想情况下,您需要为引入 WebDriverWait visible_of_element_located()
,您可以使用以下任一定位器策略:
Ideally you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
-
使用
CSS_SELECTOR
和 text 属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-test-id='add-button'][aria-label='Add to bag'] span"))).text)
使用 XPATH
和 get_attribute()
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@data-test-id='add-button' and @aria-label='Add to bag']//span"))).get_attribute("innerHTML"))
注意:您必须添加以下导入:
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考文献
链接到有用的文档:
References
Link to useful documentation:
-
get_attribute()
方法获取元素的给定属性或属性.
-
text
属性返回元素的文本.
- 使用Selenium的文本和innerHTML之间的区别
这篇关于如何使用Selenium Python在html的span数据绑定部分中获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!