Selenium 通过类选择 Div,没有这样的元素

huangapple go评论64阅读模式
英文:

Selenium Select Div By Class, no such element

问题

以下是您提供的代码的翻译部分:

我正在尝试使用Selenium打开一个网页并单击一个元素最大化屏幕按钮实际上不是按钮来最大化视频我尝试了一些通过XPath查找元素的变体但都没有奏效以下是我的代码网页是可访问的我真的只需要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)

希望这有所帮助!如果您有任何其他问题,请随时提问。

英文:

I am trying to use selenium to open a webpage and click an element (maximize screen button that's not really a button) to maximize the video. I've tried a few variations of finding element by xpath, but none have worked.. Here is my code below, the webpage is accessible.. I really just need the xpath/to know how this xpath should be formatted.. Appreciate any help !

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)

答案1

得分: 1

以下是您要翻译的代码部分:

Working code: (Note: Have used ExplicitWait and removed 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")

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
英文:

Try this XPATH expression:

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

Working code: (Note: Have used ExplicitWait and removed 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")

Imports:

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

答案2

得分: 1

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

英文:

As fullscreen mode isn't a clickable button but when you hover over on selected raw html then it is selected. So it's possible to make full screen using selenium ActionChains class as follows:

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

huangapple
  • 本文由 发表于 2023年4月7日 05:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定