我想从以下代码中提取文本“ 3351500920037”:
<div class="specs">
<h3 class="h4">Productinformatie</h3>
<dl class="specs__list">
<dt class="specs__title">
Gewicht
</dt>
<dd class="specs__value">
0,3 kg
</dd>
<dt class="specs__title">
EAN
</dt>
<dd class="specs__value">
3351500920037
</dd>
</dl>
</div>
我使用
ref_code = driver.find_element_by_xpath('//*[contains(text(),"EAN")]/following-sibling::dd').text
当我打印ref_code时,似乎仅占据文本的第一行。它显示为空。
我所拥有的:
print(ref_code)
我想要:
print(ref_code)
3351500920037
如何获取包括下一行在内的整个文本?
答案 0 :(得分:2)
这是代码,您如何从第一个搜索页面获取所有EAN编号。您可以通过首先浏览所有页面来收集所有链接来改进代码:
import selenium, csv, sys, time
from oauth2client.service_account import ServiceAccountCredentials
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
wait = WebDriverWait(driver, 20)
query = "Azzaro Chrome 100 ml"
driver.get("https://www.bol.com")
driver.find_element_by_id("searchfor").send_keys(query, u'\ue007')
# wait presence and get all product A elements
products = wait.until(ec.presence_of_all_elements_located((By.CSS_SELECTOR, "li.product-item--row a.product-title")))
# get HREF attribute from products
product_links = [product.get_attribute("href") for product in products]
# iterate through and open all product links, and get ref_code
for link in product_links:
driver.get(link)
ref_code = driver.find_element_by_css_selector("a[data-ean]").get_attribute("data-ean")
print(ref_code)
答案 1 :(得分:1)
该项目在页面上不可见,这就是visibility_of_element_located()
收到超时异常的原因。
要提取文本3351500920037
,您需要诱导WebDriverWait
和presence_of_element_located()
和get_attribute('textContent')
,它将给出您想要的结果。
print(WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, "//*[contains(.,'EAN')]/following-sibling::dd[1]"))).get_attribute('textContent'))
这是完整的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.bol.com/")
query='Azzaro Chrome 100 ml'
searchelement=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"searchfor")))
searchelement.send_keys(query)
searchelement.submit()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".product-title.px_list_page_product_click"))).click()
print(WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, "//*[contains(.,'EAN')]/following-sibling::dd[1]"))).get_attribute('textContent'))
driver.quit()