![](/img/trans.png)
[英]My javascript code won't push elements to my array and I can't figure out why?
[英]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.