如何使用xpath获取范围内的文本。硒蟒蛇

时间:2019-03-23 13:49:04

标签: python-3.x selenium xpath webdriver xpath-1.0

在问之前,我通过Google找到了2个小时的答案。但是我没有答案。

我在python中使用硒

我在下面的问题答案中使用我的代码,但未打印任何文本。

XPath query to get nth instance of an element

我想要得到的是“无法选择”

<li data-index="5" date="20190328" class="day dimmed">
    <a href="#" onclick="return false;">
        <span class="dayweek">Tuesday</span>
        <span class="day">28</span>
        <span class="sreader">Can't select</span>
    </a>
</li>

我使用xpath是因为我需要重复

我应该这样做。 上面的HTML代码是一个简单的更改

day_lists = driver.find_elements_by_xpath('//li')

没有打印内容,没有错误

for day_list in day_lists:
    print(day_list.find_element_by_xpath('//span[@class="sreader"]').text)

++++ 2019/3/24/16:45(+09:00)

当我使用以下代码进行测试

print(day_list.find_element_by_xpath('.//span[@class="sreader"]/text()'))

出现错误。为什么没有这样的元素?

selenium.common.exceptions.NoSuchElementException: 
Message: no such element: Unable to locate element: 
{"method":"xpath","selector":".//span[@class="sreader"]/text()"}

4 个答案:

答案 0 :(得分:0)

要打印所需的文本,例如从 <div class="awpcp-subtitle">Contact Information</div> <a href="https://adsler.co.uk/wp-user-test-dashboard- 2/awpcp-reply-to-ad/22/nicos-cleaning-service/"><div class="email"><span><center>&#x1F4E7</center>. </span></div></a><a href="tel:<br/><label>Phone: </label> 07576335122"><div class="phone"><span>. <center>&#128222</center></span></div></a> <a href="http://maps.google.com/?q=:<br/>. <label>Location:</label> Westminster, London, UK"><div class="location"><span><center>&#x1F4CD</center>. </span></div></a> <div class="visit-website"><Visit Website:</div> <br/><a href="http://nicoscleaningservice.co.uk" target="_blank">Visit Website</a> 标签中선택불가,您可以编写如下功能:

<span>

现在您可以按以下任意日期调用该函数:

def print_text(my_date):
        print(driver.find_element_by_xpath("//li[@class='day dimmed'][@date='" +my_date+ "']//span[@class='sreader']").get_attribute("innerHTML"))

答案 1 :(得分:0)

这是解决方案。如果这不起作用,那么我预计该元素可能出现在单独的窗口或框架中。

CSS:     li [class ='day dimmed'] [date ='20190328'] .sreader enter image description here

xpath:

enter image description here

要检查是否有多个窗口,请在下面使用

print(len(driver.window_handles))

要检查是否有多个框架,请在下面使用

print(len(driver.find_elements_by_css_selector('iframe')))

答案 2 :(得分:0)

如果未打印任何内容且没有错误,则可能隐藏了所需的文本或尚未生成该文本。

对于第一种情况,您可能需要使用get_attribute('textContent')

day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
    print(day_list.find_element_by_xpath('.//span[@class="sreader"]').get_attribute('textContent'))

第二种情况:

from selenium.webdriver.support.ui import WebDriverWait as wait

day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
    print(wait(driver, 10).until(lambda driver: day_list.find_element_by_xpath('.//span[@class="sreader"]').text)

请注意,在两种情况下,您都需要在XPath中添加前导点:

'//span' --> './/span'

答案 3 :(得分:0)

尝试以下选项:

day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[@class="day dimmed"]')))
for day_list in day_lists:
    print(day_list.find_element_by_xpath('//span[@class="sreader"]').text)

OR

day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[@class="day dimmed"]')))
for day_list in day_lists:
   print(driver.execute_script("return arguments[0].innerHTML;", day_list.find_element_by_xpath('//span[@class="sreader"]')))

请注意,您还需要跟随进口。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By