【问题标题】:Find element text using xpath in selenium-python NOt Working在 selenium-python 中使用 xpath 查找元素文本不起作用
【发布时间】:2017-01-25 11:54:46
【问题描述】:

html looks like this我已经编写了这段代码来从一个 url 中抓取所有课程。为此,我正在尝试使用xpath 计算课程数量。但它没有给我任何东西。我哪里做错了?

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait`

class FinalProject:

    def __init__(self,url= "https://www.class-central.com/subject/data-science"):`
        self.url = url
        base_url = 'https://www.class-central.com'
        self.error_flag = False
        self.driver = webdriver.Chrome(<path to chromedriver>)
        self.driver.get(self.url)
        sleep(2)
        self.count_course_and_scroll()

    def count_course_and_scroll(self):
        wait = WebDriverWait(self.driver, 30);
        ele = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Not right now, thanks.')));
        ele.click()
        print "----------------------POP UP CLOSED---------------------"
        total_courses = self.driver.find_element_by_xpath("//span[@id='number-of-courses']")
        print total_courses
        print total_courses.text
        self.driver.close()

fp = FinalProject()

【问题讨论】:

  • 请分享您工作的html代码。
  • 你有什么错误吗?
  • @Jose 我已经分享了 html 的截图。我不知道我应该如何分享完整的。
  • @Guy No errors &lt;selenium.webdriver.remote.webelement.WebElement (session="af387306f394aab0b195eb03399f4a0c", element="0.13137647096001892-2")&gt;total_courses 的输出,total_courses.text 给出空白字符串。
  • @python_user,您应该将代码共享为文本,而不是图像。还要编辑您的代码,因为存在一些缩进问题

标签: python-2.7 selenium xpath selenium-chromedriver


【解决方案1】:

如果text 不起作用,您可以尝试get_attribute

print total_courses.get_attribute('text')
#or
print total_courses.get_attribute('innerHTML')

【讨论】:

  • print total_courses.get_attribute('innerHTML') 给了我结果,但print total_courses.get_attribute('text') 给了 NOne 作为输出。为什么会这样?为什么text 不起作用?
  • @python_user 也许你可以提取父文本total_courses = self.driver.find_element_by_xpath("//span[span[@id='number-of-courses']]") print total_courses.text。它会给你187 courses available
【解决方案2】:
ele = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Not right now, thanks.')));
ele.click()
print "----------------------POP UP CLOSED---------------------"
total_courses = self.driver.find_element_by_xpath("//span[@id='number-of-courses']")

在那段代码中,我怀疑两件事:

  1. 弹出窗口总是出现吗?
  2. number-of-courses的文字是否及时显示?

如果您不确定 1.,我建议将其放入 try catch

大约 2. - 等到该元素上出现一些文本

try:
    ele = wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Not right now, thanks.')));
    ele.click()
finally:
    total_courses = wait.until(EC.presence_of_element_located(By.XPATH, "//span[@id='number-of-courses' and text() != '']")
    print total_courses.text

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-26
  • 2021-12-22
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多