Selenium选择Div By Class,没有这样的元素



我试图使用硒打开一个网页,并单击一个元素(最大化屏幕按钮,而不是真正的按钮),以最大限度地提高视频。我尝试了几种通过xpath查找元素的方法,但是都没有成功。下面是我的代码,网页是可以访问的。我只需要知道xpath应该如何格式化。谢谢你的帮助!

import time 
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions() 
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(options=options, executable_path='./driver/chromedriver')

DejeroControlVideo1 = "https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ"  
driver.get(DejeroControlVideo1)

#driver.maximize_window()
time.sleep(2)
PreviewSwitch = driver.find_element(By.XPATH, "//span[text()='ON']")
PreviewSwitch.click()
print("preview")

FullscreenButton = driver.find_element(By.XPATH, '//span[contains(concat(" ", normalize-space(@class), " "), " lh-solid ")]')[0]
FullscreenButton.click()
print("full screen")
time.sleep(5)

试试这个XPATH表达式:

(//i[contains(@ng-hide,'isFullscreen || isFullWindow')])[1]

工作代码:(注:已使用ExplicitWait,去除time.sleep())

driver.implicitly_wait(10)
DejeroControlVideo1 = "https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ"
driver.get(DejeroControlVideo1)
PreviewSwitch = driver.find_element(By.XPATH, "//span[text()='ON']")
PreviewSwitch.click()
print("preview")
FullscreenButton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "(//i[contains(@ng-hide,'isFullscreen || isFullWindow')])[1]")))
FullscreenButton.click()
print("full screen")

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

全屏模式不是一个可点击的按钮,但是当你将鼠标悬停在选定的原始html上时,它就被选中了。因此,可以使用硒ActionChains类使全屏如下:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url = 'https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ'
driver.get(url)
time.sleep(3)
fullscreen_button = driver.find_element(By.CSS_SELECTOR,"div.fr.lh-solid")
ActionChains(driver).move_to_element(fullscreen_button).click().perform()
time.sleep(2)
#driver.quit()

相关内容

  • 没有找到相关文章

最新更新