使用Selenium/Python/Chrome无法在office 365页面上找到元素

lnxxn5zx  于 2023-02-27  发布在  Go
关注(0)|答案(1)|浏览(171)

我尝试在office365环境中查找并单击电子邮件按钮。
参见图片:

我想是因为按钮在网页的工具栏/子框里,我找不到它在iframe里。
问题似乎出在下面的代码行中:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='Mail']"))).click()

HTML代码如下所示:

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\123\AppData\Local\00-Python\test.py", line 21, in <module>
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='Mail']"))).click()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\123\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
    (No symbol) [0x009A6643]
    (No symbol) [0x0093BE21]
    (No symbol) [0x0083DA9D]
    (No symbol) [0x00871342]
    (No symbol) [0x0087147B]
    (No symbol) [0x008A8DC2]
    (No symbol) [0x0088FDC4]
    (No symbol) [0x008A6B09]
    (No symbol) [0x0088FB76]
    (No symbol) [0x008649C1]
    (No symbol) [0x00865E5D]
    GetHandleVerifier [0x00C1A142+2497106]
    GetHandleVerifier [0x00C485D3+2686691]
    GetHandleVerifier [0x00C4BB9C+2700460]
    GetHandleVerifier [0x00A53B10+635936]
    (No symbol) [0x00944A1F]
    (No symbol) [0x0094A418]
    (No symbol) [0x0094A505]
    (No symbol) [0x0095508B]
    BaseThreadInitThunk [0x76D900F9+25]
    RtlGetAppContainerNamedObjectPath [0x772D7BBE+286]
    RtlGetAppContainerNamedObjectPath [0x772D7B8E+238]
j0pj023g

j0pj023g1#

请尝试使用EC.visibility_of_element_located。并尝试以下XPath表达式//div[@title="Outlook"]
下面是完整的代码,我用它工作得非常好:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url="https://www.office.com/")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//a[contains(text(),"Sign in")]'))).click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.NAME,'loginfmt'))).send_keys(f"yourusername")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,'idSIButton9'))).click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.NAME,'loginfmt'))).send_keys(f"yourusername")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.NAME,'passwd'))).send_keys(f"yourpassword")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,'idSIButton9'))).click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.ID,'idBtn_Back'))).click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//div[@title="Outlook"]'))).click()

注:以上代码仅供参考,我认为您只能使用最后一行代码(您实际点击Outlook的地方),不需要在所有代码行都使用ExplicitWait

相关问题