【问题标题】:Selenium cant find elements on pageSelenium 在页面上找不到元素
【发布时间】:2022-01-11 16:12:32
【问题描述】:

我有作业要在 python 中使用 Selenium 编写一个程序,以便在 hotmail 上发送电子邮件。一切正常,直到我不得不找到“新消息”按钮(老实说,或者该页面上的任何其他按钮) 我不断收到selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

  • 我试过了:Class,Xpath,FullXpath,Css Selector。找不到名称或 ID 使用检查器。
  • 我试着用time.sleep让它等待

我真的不知道该怎么办。 我的代码在这里:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path='C:\SeleniumDriver\chromedriver.exe')
driver.get("https://login.live.com/")
searchField = driver.find_element_by_name("loginfmt")
searchField.send_keys("")
nextButton = driver.find_element_by_id("idSIButton9").click()
passfield = driver.find_element_by_id("i0118")
passfield.send_keys("")
time.sleep(1)
signInButton = driver.find_element_by_xpath("//*[@id='idSIButton9']").click()
time.sleep(2)
try:
    nobutton = driver.find_element_by_xpath("//*[@id='idBtn_Back']").click()
finally:
    time.sleep(2)
appbutton = driver.find_element_by_xpath("//*[@id='O365_MainLink_NavMenu']").click()
time.sleep(10)
outlookbutton = driver.find_element_by_xpath("//*[@id='O365_AppTile_Mail']").click()
time.sleep(15)
newmessageButton = driver.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div[1]/div/div[1]/div/span/div/div/div/div/div[1]/div[1]").click()
time.sleep(6)

请求的 HTML:

<div class="XayzgKk2Ga7sG02AhkQKJ"><div class="_1qPqjoFrRhZTOpwH-IJ2UP"><button type="button" class="ms-Button ms-Button--icon is-checked _3YkNJYKjMvYWaDLQ_t6D9- root-157" title="Toggle Left Pane" aria-expanded="true" aria-label="Toggle Left Pane" data-is-focusable="true"><span class="ms-Button-flexContainer flexContainer-158" data-automationid="splitbuttonprimary"><i data-icon-name="GlobalNavButton" aria-hidden="true" class="ms-Button-icon icon-167"></i></span></button></div><div class="_2nxYvsT9VmpG24V7lwcfcu"><div class=""><div class=""><button type="button" class="ms-Button GJoz3Svb7GjPbATIMTlpL _2W_XxC_p1PufyiP8wuAvwF lZNvAQjEfdlNWkGGuJb7d ms-Button--commandBar Gf6FgM99ZJ9S8H48pFMdB _3o6O4cjBEmQ4Q19oQJZZiF root-168" data-is-focusable="true"><span class="ms-Button-flexContainer flexContainer-158" data-automationid="splitbuttonprimary"><i data-icon-name="ComposeRegular" aria-hidden="true" class="ms-Button-icon _1y8YJ1XNBkLjckwryuEz2e icon-172"><span role="presentation" aria-hidden="true" class="w_jrWE2b2u-icz2udikRM"><svg class="_9fiU2J67uJPVP0DBdOFMW" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.85 1.85a.5.5 0 10-.7-.7l-8 8L6 10l.85-.15 8-8z"></path><path d="M4.5 2A2.5 2.5 0 002 4.5v7A2.5 2.5 0 004.5 14h7a2.5 2.5 0 002.5-2.5v-5a.5.5 0 00-1 0v5c0 .83-.67 1.5-1.5 1.5h-7A1.5 1.5 0 013 11.5v-7C3 3.67 3.67 3 4.5 3h5a.5.5 0 000-1h-5z"></path></svg></span></i><span class="ms-Button-textContainer textContainer-159"><span class="ms-Button-label uHWG8PYRNYDO2895_TmUG label-170" id="id__6">New message</span></span></span></button></div></div></div></div>

【问题讨论】:

  • 如果使用开发工具中的检查器找不到它,selenium 也将无法找到它。你必须先在那里找到它。如果您需要帮助,您至少还必须发布一些 html。
  • 我尝试了所有连接到按钮的 xpath。这只是最后一个。
  • @Chai 我发布了按钮的 HTML 以及与之相关的任何内容。

标签: python selenium selenium-chromedriver


【解决方案1】:

“新消息”按钮将是 xpath 中的这个选择器:

"//button[contains(., 'New message')]"

此外,如果您使用记录/播放/导出工具为您生成脚本,您可能会更容易选择正确的选择器。由于您已经在使用 Python,我推荐 SeleniumBase Recorder:https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md(它应该可以帮助您更轻松地生成带有选择器的完整脚本。)

【讨论】:

  • "//button[contains(., 'New message')]" 不起作用(相同的消息)。我正在看 SeleniumBase Recorder 上的视频。我还是 python 和 Selenium 的新手,所以直到现在才听说过。谢谢。
【解决方案2】:

原因是,一旦您在第一个窗口上单击 Outlook,就会打开一个新选项卡,并且会出现一个新消息按钮。所以为了点击它,你必须确保 Selenium 正在切换到新标签。

first_win_handle = driver.current_window_handle
outlookbutton = driver.find_element_by_xpath("//*[@id='O365_AppTile_Mail']")
outlookbutton.click()

all_win_handles = driver.window_handles
driver.switch_to.window(all_win_handles[1])

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='New message']/ancestor::button"))).click()

进口:

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

【讨论】:

    【解决方案3】:

    您正试图通过 id 查找元素,但该元素没有 id。所以这是行不通的。 您要么必须确保元素获得正确的 id,要么使用不同的方式来定位它。

    您可以尝试通过 Css Selector 找到它: 'button[data-automationid="splitbuttonprimary"]'

    确保使用正确的引号。首先在检查器中尝试这个,你应该找到它。如果你不这样做:无论代码如何,它都不会工作。 如果您看到多个命中,则说明 css 不够具体,您需要找到更具体的方法来定位它。

    该选择器的基本意思是:element 类型为“按钮”,attribute“data-automationid”,值为“splitbuttonprimary”。您可以阅读有关定位特定元素、css 选择器、html 元素和属性的更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多