繁体   English   中英

使用 Python 3.7 和 Selenium,我不知道如何解决我的代码中的视口元素问题

[英]Using Python 3.7 and Selenium, I can't figure out how to troubleshoot my code for out of Viewport elements

我正在尝试使用各种方法对我的代码进行故障排除,以找出为什么我不能使用“actions.move_to_element”方法移动到屏幕外元素,然后单击它。

我正在尝试获取这行代码:

Off_Screen_Element = driver.find_element_by_class_name("c-btn-cta")

作用于这个 HTML 元素,我必须滚动查看。 接受我预订 class 时间的条件是一个按钮:

<button class="c-btn-cta c-btn-cta--chevron modal-class-action js-terms-agreement-cta" data-class-action="book-class" data-class-action-step="class-action-confirmation" data-workout-id="205911" data-club-id="229" aria-disabled="false" data-waitlistable="true">Confirm</button>

我没有收到语法错误,但是当我使用这行代码时:

print(Off_Screen_Element.text)

它什么也不返回。 没有异常或错误。 根本不值一提。 我究竟做错了什么?

我正在使用的关键代码块是这个。 请注意,有些被注释掉了我正在尝试做的故障排除。 我没有发布此脚本的大量代码,因为它工作正常。

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

#Using Firefox to access the Web
profile = webdriver.FirefoxProfile()
#options = webdriver.FirefoxOptions()

#profile.set_preference("dom.disable_beforeunload", True)
profile.set_preference("dom.webnotifications.enabled", False)
profile.update_preferences()

#profile.set_preference("dom.push.enabled", False)
driver = webdriver.Firefox(firefox_profile=profile)
driver.maximize_window()

driver.find_element_by_id("js-workout-booking-agreement-input").click()
Off_Screen_Element = driver.find_element_by_class_name("c-btn-cta")
#actions = ActionChains(driver)
#actions.move_to_element(Off_Screen_Element).click()

print(Off_Screen_Element.text)

编辑:这是整个脚本:

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#Using Firefox to access the Web
profile = webdriver.FirefoxProfile()
#options = webdriver.FirefoxOptions()

#profile.set_preference("dom.disable_beforeunload", True)
profile.set_preference("dom.webnotifications.enabled", False)
profile.update_preferences()

#profile.set_preference("dom.push.enabled", False)
driver = webdriver.Firefox(firefox_profile=profile)
driver.maximize_window()

# Open the website
driver.get('https://www.goodlifefitness.com/book-workout.html#no-redirect')
time.sleep(8)

# click on the sign in tab to get Login Window
driver.find_element_by_class_name('c-header__login-text').click()

# finding the Login Window User ID Box and sending the Login ID
User_ID = driver.find_element_by_class_name('js-login-email')
User_ID.send_keys('yyyyyyy')

#Finding the Login Window Password Box and sending the Password
Password = driver.find_element_by_class_name('js-login-password')
Password.send_keys('xxxxxxx')

#Finding the Login Window Log-In Button and Clicking on it
driver.find_element_by_class_name('js-login-submit').click()

#Pause a few seconds until the My Account Button appears
time.sleep(5)

#Find the unordered list that contains the 4th day (data-index 3) and then click on the element that is the 4th day
driver.find_element_by_xpath("//ul/li[@data-index='3']").click()

time.sleep(5)

Day_of_Timeslot = driver.find_element_by_xpath('//div[@id="day-number-4"]')
Precise_Timeslot = Day_of_Timeslot.find_element_by_xpath(".//li[@data-index='2']")
Actual_Button = Precise_Timeslot.find_element_by_xpath(".//button[@data-class-action='book-class']").click()

driver.find_element_by_id("js-workout-booking-agreement-input").click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[data-class-action='book-class']"))).click()
#Off_Screen_Element = driver.find_element_by_class_name("c-btn-cta")
#actions = ActionChains(driver)
#actions.move_to_element(Off_Screen_Element).click()```




似乎元素在页面上不可见。 使用WebDriverWait () 并等待element_to_be_clickable () 并遵循 css 选择按钮

print(WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[data-class-action='book-class']"))).text)

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[data-class-action='book-class']"))).click()

您需要导入以下库

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM