如何在节点后找到'Text'?
How to find 'Text' after node?
driver = webdriver.Chrome()
URL= ['https://makemyhomevn.com/collections/ghe-an-cafe/products/ghe-go-tron']
driver.get(URL)
sleep(1)
des = driver.find_element_by_xpath('//div[@class="product-item-description"]//strong/following sibling::text()[1]')
print(des)
我希望我的结果是'Gỗ tự nhiên',我尝试了很多方法,但无法获取'Chất liệu:'之后的文本。
您可以使用 .get_attribute('innerText')
获取 整个跨度文本 ,然后使用如下所示的 split function from Python
:
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get("https://makemyhomevn.com/collections/ghe-an-cafe/products/ghe-go-tron")
time.sleep(1)
entire_span = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Chất liệu:']/..")))
entire_span_splitted = entire_span.get_attribute('innerText').split(":")
#print(entire_span_splitted[0])
print(entire_span_splitted[1])
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
Gỗ tự nhiên.
driver = webdriver.Chrome()
URL= ['https://makemyhomevn.com/collections/ghe-an-cafe/products/ghe-go-tron']
driver.get(URL)
sleep(1)
des = driver.find_element_by_xpath('//div[@class="product-item-description"]//strong/following sibling::text()[1]')
print(des)
我希望我的结果是'Gỗ tự nhiên',我尝试了很多方法,但无法获取'Chất liệu:'之后的文本。
您可以使用 .get_attribute('innerText')
获取 整个跨度文本 ,然后使用如下所示的 split function from Python
:
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.get("https://makemyhomevn.com/collections/ghe-an-cafe/products/ghe-go-tron")
time.sleep(1)
entire_span = wait.until(EC.visibility_of_element_located((By.XPATH, "//strong[text()='Chất liệu:']/..")))
entire_span_splitted = entire_span.get_attribute('innerText').split(":")
#print(entire_span_splitted[0])
print(entire_span_splitted[1])
进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
输出:
Gỗ tự nhiên.