Webscraping: Elementnotinteractableexception While Trying To Fill The Form On This Website
I have been trying to scrape data from this site. I need to fill Get the precise price of your car form ie. the year, make, model etc.. I have written the following code till now:
Solution 1:
You can use the below approach to achieve the same.
#Set link according to data need
driver.get('http://www.indianbluebook.com/')
#Wait webpage to fully load necessary tables
def ajaxwait():
for i in range(1, 30):
x = driver.execute_script("return (window.jQuery != null) && jQuery.active")
time.sleep(1)
ifx == 0:
print("All Ajax element loaded successfully")
break
ajaxwait()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='cityPopup']/div[2]/div/div[2]/form/div[2]/div/a[1]")))
save_city = driver.find_element_by_xpath('//*[@id="cityPopup"]/div[2]/div/div[2]/form/div[2]/div/a[1]').click() #Bangalore
ajaxwait()
#fill year
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a")))
#//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year'] this is the only unique elemnt with reference to this we can find other element.#click on select year field then a dropdown will be open we will enter the year in the input box. Then select the item from the ul list.
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div/a").click()
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//input").send_keys("2017")
driver.find_element_by_xpath("//div[@class='form-group']//select[@class='form-control' and @name='manufacture_year']/following-sibling::div//em").click()
Similarly you can select other drop down by changing the @name='manufacture_year'
attribute value.
Note: Updated the code with Ajax wait.
Solution 2:
To click on BANGALORE and then select 2020 from the dropdown, you need to induce WebDriverWait for the element_to_be_clickable()
and you can use the following Locator Strategies:
Using
XPATH
:driver.get('http://www.indianbluebook.com/') WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "BANGALORE"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Select Year']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='chosen-results']//li[@class='active-result' and text()='2020']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC
Browser Snapshot:
Post a Comment for "Webscraping: Elementnotinteractableexception While Trying To Fill The Form On This Website"