【问题标题】:selenium - Not able to find input硒 - 无法找到输入
【发布时间】:2020-08-14 02:22:03
【问题描述】:

尝试创建将自动订阅新闻的脚本,但遇到了一个问题,selenium 找不到电子邮件输入和提交按钮。每次收到selenium.common.exceptions.NoSuchElementException:

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

chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")

path_to_chromedriver = 'chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path_to_chromedriver)

driver.get('https://dataengweekly.com/')

driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
email_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="email"]'))
)

email_input.send_keys("email@test.com")
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(10)

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver


    【解决方案1】:

    注意 - 您的订阅文本框位于不同的 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 我忘了他们)它的工作原理,谢谢
    • 乐于助人:)
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2019-05-05
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多