如何使用selenium填写html表单?

2024-09-30 16:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

url=http://ptvtelecom.com/ 如果您按照url并单击屏幕中间显示的“combrobar”按钮,它会将您带到需要填写的表单。我想知道如何用硒来填写表格。你知道吗

所以我已经试着用id和name来查找元素,但是没有用。例如,任何关于如何找到第一个文本框的元素的帮助都将被大大简化。你知道吗

option = webdriver.ChromeOptions()
option.add_argument(" — incognito")
browser = 
webdriver.Chrome(executable_path='/Users/grsanchez/downloads/chromedriverM', 
options=option)
browser.get('http://ptvtelecom.com/')
browser.find_element_by_xpath('//* 
[@id="cobertura"]/div/div[2]/div/div/p/a').click()

这就是问题所在

name = browser.find_element_by_id('nombre')
name.send_keys('user1')

Tags: namedivbrowsercomidhttpurl元素
2条回答

你需要切换到iframe驱动程序.switchTo().框架(“a077aa5e”)

然后在iframe中使用定位器

阅读代码中的注释,了解代码不起作用的原因。
基本上,您正在尝试选择iframe中存在的东西。你知道吗

option = webdriver.ChromeOptions()
option.add_argument(" incognito")

browser = webdriver.Chrome(executable_path='/Users/grsanchez/downloads/chromedriverM', 
options=option)

browser.get('http://ptvtelecom.com/')

## finding the button that shows the form
btn = browser.find_element_by_css_selector('#cobertura .boton-cobertura')

## using js to click it, to avoid getting issues in case the button wasn't visible
driver.execute_script("arguments[0].click();", btn)

## the element you want to select is actually inside an iframe, so we need to switch to it, if we want to select anything
driver.switch_to.frame(driver.find_element_by_css_selector('#popmake-1432 iframe'));

## selecting the name input and sending a string
name = driver.find_element_by_css_selector('#nombre')
name.send_keys('user1')

PS要返回主框架,可以执行以下操作:

driver.switch_to.default_content()

相关问题 更多 >