Python 2.7 在selenium python中使用xpath查找元素文本无效
我写这段代码是为了从url中删除所有课程。为此,我尝试使用Python 2.7 在selenium python中使用xpath查找元素文本无效,python-2.7,selenium,xpath,selenium-chromedriver,Python 2.7,Selenium,Xpath,Selenium Chromedriver,我写这段代码是为了从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_
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最终项目:
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()
def\uuuu初始化(self,url=”https://www.class-central.com/subject/data-science"):`
self.url=url
基本url=https://www.class-central.com'
self.error\u标志=False
self.driver=webdriver.Chrome()
self.driver.get(self.url)
睡眠(2)
self.count\u course\u和\u scroll()
def计数\课程\和\滚动(自):
wait=WebDriverWait(self.driver,30);
ele=等待.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'现在不行,谢谢'));
ele.click()
打印“-------------------------弹出窗口关闭------------------”
total_courses=self.driver.find_element_by_xpath(//span[@id='number-of-courses']))
打印课程总数
打印课程总数
self.driver.close()
fp=FinalProject()
如果text
不起作用,您可以尝试get\u属性
print total_courses.get_attribute('text')
#or
print total_courses.get_attribute('innerHTML')
在那段代码中,我怀疑两件事:
课程数的文本是否及时显示
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
请分享你正在处理的html代码。你有任何错误吗?@Jose我已经分享了html的截图。我不知道如何共享完整的。@Guy No errors
是total\u courses
和total\u courses的输出。text
给出空白字符串。@python\u用户,您应该将代码作为文本而不是图像共享。还可以编辑代码,因为存在一些缩进问题。print total\u courses。get\u attribute('innerHTML')
给出了结果,但print total\u courses。get\u attribute('text')
没有给出任何输出。为什么会这样?为什么text
不起作用呢?@python\u user也许你可以提取父文本total\u courses=self.driver。通过xpath(//span[span[@id='number-of-courses']])
打印total\u courses.text。它将为您提供187门可用课程
试一试
/捕获
?。。。你确定吗?:)这绝对不是一个Python
代码。谢谢,我显然不熟悉python。但想法仍然是一样的。
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