我有作业要用Selenium在python中制作一个程序,以便在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:SeleniumDriverchromedriver.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>
;新消息";按钮将是xpath:中的这个选择器
"//button[contains(., 'New message')]"
此外,如果使用录制/播放/导出工具为您生成脚本,您可能更容易选择正确的选择器。由于您已经在使用Python,我建议您使用SeleniumBase记录器:https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md(它应该可以帮助您更容易地生成带有选择器的完整脚本。(
原因是,一旦您在第一个窗口上单击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
您试图通过id查找元素,但该元素没有id。因此,这不起作用。你要么必须确保元素得到正确的id,要么使用不同的方法来定位它
您可以尝试通过Css Selector找到它:'button[data-automationid="splitbuttonprimary"]'
确保使用正确的引号。先在检查器中尝试一下,你应该会找到它。如果你不这样做:无论代码如何,它都不会工作。如果你看到多个点击,那么css不够具体,你需要找到一种更具体的方法来定位它
该选择器基本上意味着:类型为"0"的CCD_;按钮";用CCD_ 7";数据自动化";具有值";splitbuttonprimary";。你可以在这里阅读更多关于定位speccif元素、css选择器、html元素和属性的信息