为什么 selenium 中的命令会单独工作,但如果放在脚本中,selenium 不会定位元素

Why would commands in selenium works seperately but if put in script selenium does'nt locate the elements

我面临的问题是当我 运行 整个脚本时它抛出元素不可点击或找不到的错误。当我 运行 时,它会根据命令执行命令。

如果有人能解释原因以及为什么会这样,我将不胜感激。

代码示例:

driver.find_element(By.XPATH, "//div[@id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()

错误:

ElementClickInterceptedException:消息:元素点击被拦截:元素 ... 在点 (238、772) 不可点击。其他元素将收到点击:... (会话信息:chrome=101.0.4951.64) 堆栈跟踪: 回溯:

我作为我的编辑在 VS 代码中工作。

我重现了你的问题,同样的错误。我所做的修复只是在单击它之前滚动到该元素。
试试这个

driver.find_element(By.XPATH, "//div[@id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()

actions = ActionChains(driver)

button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()

我还注意到,几秒钟后网站上会出现一个弹出窗口。请确保您点击了那个按钮,因为它可能会拦截对应用按钮的点击。

更新:
这是从打开网站到选择日期并单击应用按钮的完整代码。

from selenium import webdriver 
from selenium.webdriver.chrome.service import Service 
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# Opening browser and maximizing it
driver.get("https://www.fxstreet.com/economic-calendar")
driver.maximize_window()

# Click 'Continue to site'
driver.find_element(By.CLASS_NAME, "fxs_prestitial-continue").click()

# Wait until popup appears and cancel it
driver.implicitly_wait(10)
driver.find_element(By.XPATH, "//button[@id='onesignal-slidedown-cancel-button' and text()='Cancel']").click()

# Click the datepicker button and choose date
driver.find_element(By.XPATH, "//div[@id=\'Content_C164_Col00\']/div/div/div[2]/div/div/div/div/div/button/span/span/span[3]").click()
driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")[1].click()

# Creating actions instance
actions = ActionChains(driver)

# Scrolling to 'Apply' button and clicking it
button = driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight")
actions.move_to_element(button)
button.click()

我测试了一个有效的新版本:

WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.ID, "onesignal-slidedown-cancel-button"))).click()

while True:
    try:
        studio = driver.find_element(By.CLASS_NAME, "fxs_headline_tiny").text
        # print(studio)
        driver.execute_script("window.scrollTo(0, 450)")
        driver.find_element(By.CLASS_NAME, "fxs_icon.fa-calendar-alt.fa-w-14").click()
        time.sleep(0.5)
        exact_date = driver.find_elements(By.CLASS_NAME, "fxs_c_datepicker_button")
        for i in range(len(exact_date)):
            exact_date_i = exact_date[i].text
            if(exact_date_i == "Today"):
                time.sleep(2)
                exact_date[i].click()
                break

            
        break
    except:
        print("Studio not found")
driver.find_element(By.CLASS_NAME, "fxs_btn.fxs_btn_cta.fxs_fRight").click()