【问题标题】:Why is Selenium not finding an element为什么硒找不到元素
【发布时间】:2021-09-19 17:19:41
【问题描述】:

我编写了这个短程序来登录我的邮局:

import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.web.de/')
time.sleep(2)
frame = driver.find_element_by_name('landingpage')
  # do something with frame ...
driver.switch_to.frame(frame)
innerFrame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(innerFrame)
driver.find_element_by_id("save-all-conditionally").click()
time.sleep(2)
driver.switch_to.default_content()
time.sleep(3)
inputemail = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[2]/div/input')
inputpwd = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/div[3]/div/input')
buttonsend = driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/section[1]/div/form/button')
inputemail.send_keys('xxx@web.de')
inputpwd.send_keys('xxx')
buttonsend.click()
time.sleep(3)
frame2 = driver.find_element_by_name('home')
driver.switch_to.frame(frame2)
linkwrite = driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[2]/div[1]/div[6]/div[1]/ul/li/section/div[3]/div/div[1]/a')
linkwrite.click()

这部分与那里的 iframe 配合得很好。我的下一个目标是填写一个输入字段。签到后打开的页面Code就是图片中的那个:https://www.transfernow.net/dl/20210919Porfd5N7 但填写代码:

frame3 = driver.find_element_by_xpath('...')
driver.switch_to.frame(frame3)
fillin = driver.find_element_by_xpath('/html/body/div[3]/div[3]/div[3]/div[1]/div[1]/div/form/div[2]/div[1]/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input')
fillin.send_keys('hello')

导致: "消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"/html/body/nav/div[2]/div[1]/div[2]/a[ 1]"}" 我的错在哪里? 请帮帮我!

【问题讨论】:

    标签: python windows selenium


    【解决方案1】:

    几件事

    1. 有嵌套的iframe,第一个是iframe[name='landingpage'](由css选择器定位)第二个是iframe[sandbox*='allow-popups'][style^='display']然后你可以点击Zustimmen und weiter按钮。

    2. 您使用的xpath绝对的,请尝试使用relative xpath,如果可能切换到css_selector

    3. 使用显式等待。

    代码:-

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    driver.implicitly_wait(30)
    wait = WebDriverWait(driver, 20)
    driver.get("https://web.de/consent-management/")
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[name='landingpage']")))
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[sandbox*='allow-popups'][style^='display']")))
    wait.until(EC.element_to_be_clickable((By.ID, "save-all-conditionally"))).click()
    driver.switch_to.default_content()
    
    inputemail = wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginUsername")))
    inputpwd =  wait.until(EC.element_to_be_clickable((By.ID, "freemailLoginPassword")))
    buttonsend = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Login']")))
    inputemail.send_keys('xxx@web.de')
    inputpwd.send_keys('xxx')
    buttonsend.click()
    time.sleep(3)
    

    进口:

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

    此外,您还可以使用以下代码:

    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input"))).send_keys('WEB.DE E-Mail-Adresse')
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.pos-input input[name^='password']"))).send_keys('password here')
    

    Bitte erneut einloggen页面上填写详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多