Find element text using xpath in selenium-python NOt Working

微笑、不失礼 提交于 2019-12-12 04:35:09

问题


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


回答1:


If text doesn't work you can try get_attribute

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



回答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']")

In that piece of code, I suspicious 2 things:

  1. Does the popup always appear?
  2. Does the text of number-of-coursesshow in time?

If you are not sure about 1., I would recommend to put it in a try catch

And about 2. - wait until some text appears on that element

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


来源:https://stackoverflow.com/questions/41850994/find-element-text-using-xpath-in-selenium-python-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!