Python 3.x selenium-无法找到输入
试图创建自动订阅新闻的脚本,但遇到问题,Python 3.x selenium-无法找到输入,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,试图创建自动订阅新闻的脚本,但遇到问题,selenium无法找到电子邮件输入和提交按钮。每次获取selenium.common.exceptions.NoSuchElementException: 从selenium导入webdriver 从selenium.webdriver.chrome.options导入选项 从selenium.webdriver.common.by导入 从selenium.webdriver.common.keys导入密钥 从selenium.webdriver.sup
selenium
无法找到电子邮件输入和提交按钮。每次获取selenium.common.exceptions.NoSuchElementException:
从selenium导入webdriver
从selenium.webdriver.chrome.options导入选项
从selenium.webdriver.common.by导入
从selenium.webdriver.common.keys导入密钥
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
chrome_options=options()
chrome_选项。添加_参数(“--window size=1920x1080”)
路径到chromedriver='chromedriver'
driver=webdriver.Chrome(Chrome\u选项=Chrome\u选项,可执行路径=路径到chromedriver)
司机,上车https://dataengweekly.com/')
驱动程序。通过标签名称(“正文”)查找元素。发送键(键。向下翻页)
电子邮件输入=WebDriverWait(驱动程序,10)。直到(
EC.元素的存在位置((By.CSS_选择器,'input[type=“email”]”)
)
电子邮件输入。发送密钥(“email@test.com")
驱动程序。通过css选择器(“按钮.订阅btn”)查找元素。单击()
时间。睡眠(10)
注意-您的订阅文本框位于不同的iframe
中,要使用该iframe
,您需要首先切换到该iframe
尝试下面的代码,让我知道如果你需要更多的澄清-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get('https://dataengweekly.com/')
iframe = driver.find_element_by_xpath('//iframe')
driver.switch_to.frame(iframe)
email_input = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@type='email']")))
action.move_to_element(email_input).click().send_keys("email@test.com").perform()
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(2)
iframe
我忘了它们)它很有效,谢谢你的帮助:)