如何使用Selenium查找页面元素?

问题描述 投票:0回答:1

enter image description here

driver=webdriver.Chrome()
driver.get("https://compass.tinkoff.ru/")
driver.maximize_window()
driver.implicitly_wait(20)

elem = driver.find_element(By.XPATH, '//button[text()="Открыть 
полную карту"]').click()
time.sleep(20)

phone = driver.find_element(By.XPATH, "//[@automation-id='phone- 
input']")
phone.send_keys("12345678")
time.sleep(20)

所以我添加了一张html照片,需要找到该元素并在空字段中添加电话号码。无论我尝试什么(通过 ID 或 CLASS_NAME),都找不到元素/

python html selenium-webdriver element
1个回答
0
投票

您要查找的元素位于

iframe

试试这个。

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

options = Options()
options.add_argument("start-maximized")   
driver = webdriver.Chrome(options=options)        
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@id='app']")))

driver.maximize_window()

# switch to the iframe..
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe"))

# using wait for the element..
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//button[.='Открыть всю карту']"))).click()

# switch to the main window ...
driver.switch_to.default_content()

phone_input = driver.find_element(By.XPATH, "//input[@role='textbox']")
phone_input.send_keys("99999999")
© www.soinside.com 2019 - 2024. All rights reserved.