繁体   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