繁体   English   中英

无法定位元素硒 Python

[英]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,但没有奏效。

元素的html主体

但它会抛出一个错误:无法定位元素

您在iframe元素,您需要先切换它。

这是iframe定位器: By.CLASS_NAME -> iframe_bpm

您可以使用组合WebDriverWaitexpected_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.

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