Home > OS >  Why would commands in selenium works seperately but if put in script selenium does'nt locate th
Why would commands in selenium works seperately but if put in script selenium does'nt locate th

Time:05-27

The problem i am facing is when i run the whole script it throws error of element not clickable or not found. While when i run it command per command it works.

If anyone can explain the reason and why it behaves this way i ll be very grateful.

codeExample:

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()

ERROR:

ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (238, 772). Other element would receive the click: ... (Session info: chrome=101.0.4951.64) Stacktrace: Backtrace:

I am working in VS code as my editor.

CodePudding user response:

I reproduced your problem and had the same error. What i did to fix it is just scroll to the element before clicking it.
Try this out

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()

I also noticed that after a few seconds a popup appears on the website. Make sure you click that one away as it may intercept the click on the apply button.

Update:
Here's the full code from opening the website to selecting date and clicking the apply button.

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()

CodePudding user response:

I tested a new version that works:

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()
  • Related