简体   繁体   English

Selenium 在页面上找不到元素

[英]Selenium cant find elements on page

I have homework to make a program in python with Selenium to send an email on hotmail.我有作业要在 python 和 Selenium 中编写一个程序,以便在 hotmail 上发送 email。 Everything was working fine till I had to find the "New Message" button (or any other button on that page to be honest) Im constantly getting selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:一切正常,直到我不得不找到“新消息”按钮(老实说,或者该页面上的任何其他按钮)我不断收到selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

  • I tried By: Class,Xpath,FullXpath,Css Selector .我试过了: Class,Xpath,FullXpath,Css Selector Can't find the name or Id using the inspector.无法使用检查器找到名称或 ID。
  • I tried to make it wait with time.sleep我试图让它等待time.sleep

I really don't know what to do.我真的不知道该怎么办。 My code is here:我的代码在这里:

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)

Requested HTML:请求的 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>

The "New message" button would be this selector in xpath: “新消息”按钮将是 xpath 中的这个选择器:

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

Also, you might have an easier time choosing the correct selector if you use a record/playback/export tool to generate a script for you.此外,如果您使用记录/播放/导出工具为您生成脚本,您可能会更容易选择正确的选择器。 Since you're already using Python, I recommend the SeleniumBase Recorder: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md (It should help you generate a full script with selectors more easily.)由于您已经在使用 Python,我推荐 SeleniumBase 记录器: https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md (它应该可以帮助您更轻松地生成带有选择器的完整脚本)

The reason is, once you click on outlook on first windows a new tab is opened and there we have a new message button.原因是,一旦您在第一个 windows 上单击 outlook,就会打开一个新选项卡,并且我们有一个新消息按钮。 So in order to click on that, you would have to make sure that Selenium is switching to the new tab.因此,为了单击它,您必须确保 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()

Imports:进口:

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

You are trying to find the element by id, but the element does not have an id.您试图通过 id 查找元素,但该元素没有 id。 So that does not work.所以这是行不通的。 You will either have to ensure the element gets that correct id, or use a different way to locate it.您要么必须确保元素获得正确的 id,要么使用不同的方式来定位它。

You might try finding it by Css Selector: 'button[data-automationid="splitbuttonprimary"]'您可以尝试通过 Css 选择器找到它: 'button[data-automationid="splitbuttonprimary"]'

Make sure to use the right quotes.确保使用正确的引号。 Try this in the inspector first and you should find it.首先在检查器中尝试这个,你应该找到它。 If you do not: it will not work, no matter the code.如果你不这样做:无论代码如何,它都不会工作。 If you see multiple hits then the css is not specific enough, and you will need to find a more specific way to locate it.如果您看到多个命中,则 css 不够具体,您需要找到更具体的方法来定位它。

That selector basically means: an element of type "button" with attribute "data-automationid" with value "splitbuttonprimary".该选择器基本上意味着:“按钮”类型的element ,其attribute为“data-automationid”,值为“splitbuttonprimary”。 You can read more about locating specicif elements, css selectors, html elements and attributes here您可以在此处阅读有关定位特定元素、css 选择器、html 元素和属性的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM