切换窗口后找不到元素



我正在尝试登录medium.com,仅用于使用硒的实验目的,以便我可以访问优质文章。我能够点击登录页面上的电子邮件登录,但我似乎不能得到硒找到之后出现的文本框。这是我的理解,这个文本框出现在同一页面上的另一个窗口,所以我把驱动程序切换到第二个窗口,在点击电子邮件登录后出现在驱动程序处理程序中,仍然没有。

import requests as re
from bs4 import BeautifulSoup
import pymongo
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.headless = False
driver = webdriver.Chrome(executable_path = "C:\UsersDominickDesktopchromedriver.exe", options = options)
driver.get('https://medium.com/m/signin')
driver.find_element_by_id("email-susi-button-text").click()
window = driver.window_handles[1].replace('CDwindow-', '')
driver.switch_to.window(driver.window_handles[1])
try:
#inputdiv = driver.find_element_by_xpath('//aria-label[1]')
#inputdiv = driver.find_element_by_class_name('bs b bt bu dj')
#inputdiv = driver.find_element_by_tag_name('input')
#inputdiv = driver.find_element_by_class_name('eo al')
inputdiv = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CLASS_NAME, 'eo al')))
print('Success')
except NoSuchElementException:
print('Element not found')

try/except语句中所有注释掉的代码都是我用来为文本框查找匹配HTML的不同方法。当我设置刹车点并观察司机时。页面源的HTML显示包含了我正在寻找的一切,但我似乎无法正确地定位它与硒。

这是我从驱动程序复制的原始HTML。交互模式或带有断点的页面资源

<iframe src="https://a16180790160.cdn.optimizely.com/client_storage/a16180790160.html" hidden="" tabindex="-1" title="Optimizely Internal Frame" height="0" width="0" style="display: none;"></iframe><div><div role="dialog" aria-modal="true" class="l m n o p q r s t u v w x y z" tabindex="-1"><div class="ab ac ae af ag ah"><div class="ai aj ak al am an ao ap aq ar as"><div class="at" id="susi-modal-background"><div class="ai au av aw l ax ay az ba bb bc"><div class="bd be l m bf n bg bh bi bj bb"><div class="at" id="susi-modal-header"><h2 class="bk b bl bm bn bo bp">Sign in with email</h2></div><div class="at" id="susi-modal-subheader"><div class="bq br al g"><h4 class="bs b bt bu bv">Enter the email address associated with your account, and we’ll send a magic link to your inbox.</h4></div></div><div class="bw al"><div class="l m bf"><div class="du by al"><div class=""><div class="el eg l m bf bh"><div class="bx al em en"><p class="bs b di cb bv"><div class="">Your email</div></p></div><div class="am"><div class="eo al"><p class="bs b bt bu dj"><input aria-label="email" aria-invalid="false" aria-describedby="error" class="dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej bh bv ek" pattern=".*" type="text" value=""></p></div></div></div></div></div><div class="ep by al"><button class="bs b ca cb eq cc er es et eu ev ch ew ex ey ez ci fa ck cl cm cn co">Continue</button></div><div class="fb du by al"><p class="bs b ca cb dj"><button class="cs ct cu cv cw cx cy cz da ch db dc dd de df"><svg class="dr ds dt" width="19" height="19" viewBox="0 0 19 19"><path d="M11.47 13.97L6.99 9.48 11.47 5l.55.5-3.99 3.98 4 4z" fill-rule="evenodd"></path></svg>All sign in options</button></p></div></div></div></div></div></div></div></div></div></div></body></html>

我会说,然而,当我打印(driver.pagesource)时,打印的HTML不包含观察活动变量驱动程序时显示的相同的HTML。WTV的页面资源。当我print()页面源代码时,HTML似乎指示了第一个窗口。我不确定发生了什么,因为在实时模式下显示变量表明司机。pagesource包含我正在寻找在适当的时间被定位的HTML。我甚至尝试使用WebDriverWait如上所示,以确保我正在寻找的元素及时加载,但这只是超时,因为它没有找到元素。

当我们试图在新打开的浏览器窗口中查找元素时,需要切换到新窗口

当单击Sign in with email选项时,文本框元素出现在同一窗口中。

尝试如下并确认:

driver.get("https://medium.com/m/signin")
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.ID,"email-susi-button-clickable"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@aria-label='email']"))).send_keys("sample@gmail.com")

最新更新