英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论