问题
The Html from where i want to extract data is:
<div class="infoMessageInner">
<p>
<span ng-bind-html="servicesCtrl.texts.addressInfo" class="ng-binding">
Fiber är beställd till adressen. Tjänsterna kan du beställa när installationen är färdig.
</span>
<span ng-show="servicesCtrl.address.status === 'Orderable'" class="ng-hide">
<a ng-click="servicesCtrl.preScrollToVerticalPosition('checkout', 'checkout', '/checkoutFiber', undefined, 'Checkout fiber')">Till fiberbeställningen
</a>
</span>
<span ng-show="servicesCtrl.address.status === 'Inquireable'" class="ng-hide">
<a ng-click="servicesCtrl.preScrollToVerticalPosition('checkout', 'checkout', '/inquiry', undefined, 'Inquiry')">Till intresseanmälan</a>
</span>
</p>
</div>
**I want to extract data just from the 1st span only.So far i already did post and get the solution but again problem persist i'm getting empty list of selenium web elements. **
Code snippet is:(using Python-Selenium)
driver.find_elements_by_css_selector('div.infoMessageInner > p')
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.infoMessageInner > p')))
data = driver.find_elements_by_css_selector('div.infoMessageInner > p')
please help and sorry for posting such question because i'm learning selenium.
回答1:
To extract the text Fiber är beställd till adressen. Tjänsterna kan du beställa när installationen är färdig. just from the 1st span only you need to induce WebDriverWait for the text to be present in the element and you can use the following solution:
Imports:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Line of code:
#Option 1 - text_to_be_present_in_element and CSS_SELECTOR element = WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, "div.infoMessageInner>p>span.ng-binding"), "Fiber är beställd till adressen")) #Option 2 - text_to_be_present_in_element_value and CSS_SELECTOR element = WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR, "div.infoMessageInner>p>span.ng-binding"), "Fiber är beställd till adressen")) #Option 3 - text_to_be_present_in_element and XPATH element = WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element((By.XPATH, "//div[@class='infoMessageInner']/p/span[@class='ng-binding']"), "Fiber är beställd till adressen")) #Option 4 - text_to_be_present_in_element_value and XPATH element = WebDriverWait(driver, 20).until(EC.text_to_be_present_in_element_value((By.XPATH, "//div[@class='infoMessageInner']/p/span[@class='ng-binding']"), "Fiber är beställd till adressen"))
Text will be:
Fiber är beställd till adressen. Tjänsterna kan du beställa när installationen är färdig.
回答2:
If you have no more <span>
elements in html, you can just find first span element with driver.find_element_by_tag_name('span').text
回答3:
You can use below bindings in CSS selector
.ng-binding
Edit:
driver.find_element_by_css_selector('.ng-binding').text
来源:https://stackoverflow.com/questions/50906710/how-to-extract-data-from-the-following-html