[英]Unable to locate element selenium Python
我正在嘗試使用以下代碼填寫表格:
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')
我試圖查看是否有 iframe,但沒有奏效。
但它會拋出一個錯誤:無法定位元素
您在iframe
元素,您需要先切換它。
這是iframe
定位器: By.CLASS_NAME -> iframe_bpm
。
您可以使用組合WebDriverWait
和expected_conditions
然后使用frame_to_be_available_and_switch_to_it
方法進行切換。
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')
以下導入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
有一個iframe需要先切換。使用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')
使用以下進口。
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.