Issue
I want to scrape data from the timberland website: https://www.timberland.com/en-us/store-locator.
First I need to connect to the website and then I want to locate the search element. I am able to see the search element in the html code but program throws me an error when i try to access it:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="iframe_name_or_id"]"}
And I have tried wait method using:
web = 'https://www.timberland.com/en-us/store-locator'
driver.get(web)
driver.implicitly_wait(5)
search_box = driver.find_element(By.CLASS_NAME, "search-box")
Solution
Root cause of the issue: Desired element(in your case search box) is within an IFRAME. Hence you can't access it directly.
Solution: You need to switch into the IFRAME context first, and then try to perform action on the desired element.
Check the working code with in-line explanation below:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome()
web = 'https://www.timberland.com/en-us/store-locator'
driver.get(web)
driver.maximize_window()
# Below line creates wait object with 10s wait time
wait = WebDriverWait(driver, 10)
# Below line switches into the IFRAME
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '(//iframe)[1]')))
# Now perform all actions within the IFRAME
search_box = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "search-box")))
search_box.send_keys("mic test 123")
# Once actions are performed, use below code to come out of IFRAME
driver.switch_to.default_content()
time.sleep(10)
Answered By - Shawn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.