[英]Using Python 3.7 and Selenium, I can't figure out how to troubleshoot my code for out of Viewport elements
I am trying to use various methods to troubleshoot my code to figure out why I can't use the "actions.move_to_element" method to move to an offscreen element, and click on it.我正在尝试使用各种方法对我的代码进行故障排除,以找出为什么我不能使用“actions.move_to_element”方法移动到屏幕外元素,然后单击它。
I am trying to get this line of code:我正在尝试获取这行代码:
Off_Screen_Element = driver.find_element_by_class_name("c-btn-cta")
to act on this HTML element, which I must scroll to see.作用于这个 HTML 元素,我必须滚动查看。 It is a button to accept the conditions of me booking a class time:
接受我预订 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>
I get no syntax error, but when I use this line of code:我没有收到语法错误,但是当我使用这行代码时:
print(Off_Screen_Element.text)
it returns nothing.它什么也不返回。 No exception or error.
没有异常或错误。 Just nothing.
根本不值一提。 What am I doing wrong?
我究竟做错了什么?
The key chunk of code I am using is this.我正在使用的关键代码块是这个。 Note, some is commented out for trouble-shooting I am trying to do.
请注意,有些被注释掉了我正在尝试做的故障排除。 There is a large chunk of code of this script I am not posting, as it is working fine.
我没有发布此脚本的大量代码,因为它工作正常。
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)
Edit: This is the entire script:编辑:这是整个脚本:
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()```
It seems element is not visible on the page.似乎元素在页面上不可见。 Use
WebDriverWait
() and wait for element_to_be_clickable
() and following css selector for the button使用
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()
You need to import below libraries您需要导入以下库
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.