在 selenium-python 中使用 xpath 查找元素文本不起作用 [英] Find element text using xpath in selenium-python NOt Working

查看:103
本文介绍了在 selenium-python 中使用 xpath 查找元素文本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

html 看起来像这样我编写了这段代码来从网址.为此,我正在尝试使用 xpath 获取课程数量.但它没有给我任何东西.我哪里做错了?

html looks like thisI have written this code to scrape all courses from a url. For this I am trying to get the count of courses using xpath. But it does not give me anything. Where I am doing wrong?

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()

推荐答案

如果 text 不起作用你可以试试 get_attribute

If text doesn't work you can try get_attribute

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

这篇关于在 selenium-python 中使用 xpath 查找元素文本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆