簡體   English   中英

raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: for send whatsapp to all conatcts

[英]raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: for sending whatsapp to all conatcts

我正在嘗試在 Windows 7 上的 Python 中運行以下代碼。

我收到以下錯誤。

回溯(最后一次通話):文件“C:\Users\Yogesh Wadhwa\Desktop\whatsapp 2.py”,第 41 行,在 group_title = wait.until(EC.presence_of_element_located(( File "C:\Users\Yogesh Wadhwa \AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引發 TimeoutException(消息、屏幕、堆棧跟蹤)selenium.common.exceptions。超時異常:消息:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import sys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import datetime
import time
import openpyxl as excel


driver = webdriver.Chrome("C:\\Users\\Yogesh Wadhwa\\AppData\\Local\\Programs\\Python\\Python38-32\\chromedriver.exe")
def readContacts(fileName):
    lst = []
    file = excel.load_workbook(fileName)
    sheet = file.active
    firstCol = sheet['A']
    for cell in range(len(firstCol)):
        contact = (firstCol[cell].value).encode('utf-8')
        lst.append(contact)
    return lst


targets = readContacts("C:\\Users\\Yogesh Wadhwa\\AppData\\Local\\Programs\\Python\\Python38-32\\HNI.xlsx")
#print(targets)

driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 600)
wait5 = WebDriverWait(driver, 5)
msg = input('Enter the message : ')
#targets =['Mona 1','School']
for target in targets:
    x_arg = '//span[contains(@title,' +'"' +target.decode('utf-8') + '"' +')]' #.decode('utf-8') 
    group_title = wait.until(EC.presence_of_element_located((
        By.XPATH, x_arg)))
    group_title.click()


    message = driver.find_element_by_class_name('_2EoyP')
    message.send_keys(msg)

    sendbutton = driver.find_element_by_class_name('_2FVVk')    
    sendbutton.click()
    time.sleep(5)
driver.close()

當您使用顯式等待時,它會將問題埋在該超時錯誤中。 您無法判斷它是否沒有找到您的 object 或者它需要很長時間才出現。

對您的代碼的一些想法:

  • 您的 XPath 看起來不正確。 您至少缺少一個方括號。 試試這個,而不是代碼中的那個:

'//span[contains(@title,"' +target + '" )]'

  • 我建議您在運行時驗證x_arg的值。 把它打印出來,這樣你就可以確保字符串是你所期望的。

  • 潛在地,您的代碼僅在您取消注釋您的targets數組時才有效。

  • 您正在for循環中構建 XPath - 沒有任何附加值。 您動態構建的字符串不會填充任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM