在Python上使用Selenium查找元素

时间:2018-10-09 22:50:56

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

我正在尝试在当前幻灯片结束时自动点击大学在线讲座上的“下一页”,因为这需要用户在幻灯片结束时手动按下“下一页”。

将硒与python结合使用,可以成功访问网页,登录并导航至讲座幻灯片本身,但无法继续进行学习

HTML Element on pastebin尝试在3397行上添加元素

我正在尝试获取elapsedTime / totalTime

while ( currentSlide != totalSlide )
    if elapsedTime == totalTime
        find and click on 'next'

我尝试过:

duration = driver.find_element(By.XPATH,"//div[@class='label time'][@style='display: none;]")
duration = driver.find_element(By.XPATH,"//div[@class='.label.time'][@style='display: none;']")

任何帮助将不胜感激!

编辑:

让Lukas建议工作正常

content = urllib.request.urlopen(URL, timeout=10).read().decode("utf-8") duration = content.split("<div class="label time" style="display: none;">")[1].split("</div>")[0]

2 个答案:

答案 0 :(得分:0)

我会尝试:

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

browser = webdriver.Firefox()

browser.get(URL)
delay = 30  # seconds
WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'label time')))
print("Page is ready!")
# duration = browser.find_element_by_class("label time").text
# --> "InvalidSelectorException: Message: invalid selector: Compound class names not permitted"
duration = browser.find_element_by_css_selector(".label.time").text

# alternative:
duration = driver.find_element_by_xpath("//*[@class='label time']").text

答案 1 :(得分:0)

您是否尝试使用普通解析器代替硒? 检查一下:

content = urllib.request.urlopen(URL, timeout=10).read().decode("utf-8")
duration = content.split("<div class="label time" style="display: none;">")[1].split("</div>")[0]