简体   繁体   中英

Unable to locate element selenium Python

I am trying to fill in a form using this code:

from selenium import webdriver
import time

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator- 
doorverkoop')

time.sleep(5)

#browser.switch_to_frame(browser.find_element_by_id("_hjRemoteVarsFrame"))  

kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')

I tried to see if there is an iframe but it didn't work.

元素的html主体

But it drops an error : Unable to locate element

Your element inside iframe , you need switch it first.

This is the iframe locator: By.CLASS_NAME -> iframe_bpm .

You can use combination WebDriverWait and expected_conditions then utilize frame_to_be_available_and_switch_to_it method to switch.

browser = webdriver.Chrome( chrome_options=chrome_options)
browser.get('https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop')
browser.maximize_window()
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, 'iframe_bpm')))

kenteken = browser.find_element_by_id('InputControl_txtKenteken')
kenteken.send_keys('simple text')

Following import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

There is an iframe you need to switch it first.Use WebDriverWait.

driver.get("https://www.vwe.nl/Autobedrijf/In-en-verkoop/bpm-calculator-doorverkoop")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'.iframe_bpm')))
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"InputControl_txtKenteken"))).send_keys('text')

use following imports.

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM