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

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

Selenium Select Div By Class, no such element

问题

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

  1. 我正在尝试使用Selenium打开一个网页并单击一个元素最大化屏幕按钮实际上不是按钮来最大化视频我尝试了一些通过XPath查找元素的变体但都没有奏效以下是我的代码网页是可访问的我真的只需要XPath或者知道应该如何格式化这个XPath感谢任何帮助
  2. import time
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.chrome.options import Options
  6. options = webdriver.ChromeOptions()
  7. options.add_experimental_option('excludeSwitches', ['enable-logging'])
  8. options.add_experimental_option('excludeSwitches', ['enable-automation'])
  9. options.add_experimental_option("useAutomationExtension", False)
  10. driver = webdriver.Chrome(options=options, executable_path='./driver/chromedriver')
  11. DejeroControlVideo1 = "https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ"
  12. driver.get(DejeroControlVideo1)
  13. # driver.maximize_window()
  14. time.sleep(2)
  15. PreviewSwitch = driver.find_element(By.XPATH, "//span[text()='ON']")
  16. PreviewSwitch.click()
  17. print("preview")
  18. FullscreenButton = driver.find_element(By.XPATH, '//span[contains(concat(" ", normalize-space(@class), " "), " lh-solid ")][0]')
  19. FullscreenButton.click()
  20. print("full screen")
  21. 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 !

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.chrome.options import Options
  5. options = webdriver.ChromeOptions()
  6. options.add_experimental_option('excludeSwitches', ['enable-logging'])
  7. options.add_experimental_option('excludeSwitches', ['enable-automation'])
  8. options.add_experimental_option("useAutomationExtension", False)
  9. driver = webdriver.Chrome(options=options, executable_path='./driver/chromedriver')
  10. DejeroControlVideo1 = "https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ"
  11. driver.get(DejeroControlVideo1)
  12. #driver.maximize_window()
  13. time.sleep(2)
  14. PreviewSwitch = driver.find_element(By.XPATH, "//span[text()='ON']")
  15. PreviewSwitch.click()
  16. print("preview")
  17. FullscreenButton = driver.find_element(By.XPATH, '//span[contains(concat(" ", normalize-space(@class), " "), " lh-solid ")]')[0]
  18. FullscreenButton.click()
  19. print("full screen")
  20. time.sleep(5)

答案1

得分: 1

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

Working code: (Note: Have used ExplicitWait and removed time.sleep() )

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

Imports:

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

Try this XPATH expression:

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

Working code: (Note: Have used ExplicitWait and removed time.sleep() )

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

Imports:

  1. from selenium.webdriver.support.ui import WebDriverWait
  2. 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:

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from webdriver_manager.chrome import ChromeDriverManager
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.common.action_chains import ActionChains
  7. options = webdriver.ChromeOptions()
  8. options.add_argument("start-maximized")
  9. options.add_experimental_option("detach", True)
  10. driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
  11. url = 'https://control.dejero.com/return_video/9I_HsFn7NYHiKvtJbHa3WQ'
  12. driver.get(url)
  13. time.sleep(3)
  14. fullscreen_button = driver.find_element(By.CSS_SELECTOR,"div.fr.lh-solid")
  15. ActionChains(driver).move_to_element(fullscreen_button).click().perform()
  16. time.sleep(2)
  17. #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:

确定