Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果选项卡在python中不可见,Selenium将无法正常工作_Python_Selenium_Selenium Chromedriver - Fatal编程技术网

如果选项卡在python中不可见,Selenium将无法正常工作

如果选项卡在python中不可见,Selenium将无法正常工作,python,selenium,selenium-chromedriver,Python,Selenium,Selenium Chromedriver,我创建了一个浏览器抓取脚本,它使用python中的selenium在WhatsApp web上发送一条消息,但昨天注意到一个问题,它发送了一半消息,或者没有发送消息。调试后,发现浏览器窗口必须处于活动状态才能发送消息我的发送消息代码如下 def send_message(msg): whatsapp_msg = driver.find_element_by_class_name(send_messageClass) for part in msg.split('\n'):

我创建了一个浏览器抓取脚本,它使用python中的selenium在WhatsApp web上发送一条消息,但昨天注意到一个问题,它发送了一半消息,或者没有发送消息。调试后,发现浏览器窗口必须处于活动状态才能发送消息我的发送消息代码如下

def send_message(msg):
    whatsapp_msg = driver.find_element_by_class_name(send_messageClass)
    for part in msg.split('\n'):
        whatsapp_msg.send_keys(part)
        ActionChains(driver).key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.SHIFT).key_up(Keys.ENTER).perform()
    time.sleep(1)    
    ActionChains(driver).send_keys(Keys.RETURN).perform()
    time.sleep(1)

find\u element\u by\u class\u name
只需从DOM中检索元素。如果它是可见的,则无法确保

对于此用途以及图元在预期条件下的可见性:

selenium.webdriver.support.expected_conditions.visibility_of(element)
这将等待元素可见,直到达到指定的超时。下面是一个超时时间为60秒的示例:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EXP_CON

...

wait = WebDriverWait(driver, 60)
whatsapp_msg = driver.find_element_by_class_name(send_messageClass)
visible_whatsapp_msg = wait.until(EXP_CON.visibility_of(whatsapp_msg))

ActionChains不工作,在浏览器不可见时不写入文本<代码>发送键不在不可见元素上运行。对此,除了确保元素在尝试输入文本之前可见之外,您无能为力。