如何在 Selenium 中关闭弹出窗口

How to close pop up window in Selenium(如何在 Selenium 中关闭弹出窗口)
本文介绍了如何在 Selenium 中关闭弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法关闭 http://www.cargo.lt/ 之后出现的弹出窗口负载.这是我得到的:

Can't close pop up window which appears right after http://www.cargo.lt/ loads. Here's what I've got:

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('http://www.cargo.lt/asp/index.asp?')
time.sleep(10)
driver.find_element_by_xpath('/html/body/div[36]/div/a').click()

我对如何编写自定义 xpath/css 路径不是很熟悉,现在只需单击检查元素并复制 xpath.我做错了什么?

I'm not very familiar with how to write custom xpath/css path and now just clicked on Inspect element and copied xpath. What I'm doing wrong?

多么愚蠢的错误.没有意识到当元素离开屏幕时 Selenium 无法点击它.刚刚添加了 driver.maximize_window() ,我所有的问题都消失了.谢谢大家的回答.不幸的是我还不能投票,因为我没有足够的积分......

What a stupid mistake. Didn't realize that when element is off screen Selenium can't click on it. Just added driver.maximize_window() and all my problems are gone. Thanks all for your answers. Unfortunately I can't vote yet, because I don't have enough points...

推荐答案

我试过你的代码它工作正常我不知道你有什么问题但是试试这个:driver.implicitly_wait(10)插入:

i tried your code it's work fine i don't know whats you problem but try this : driver.implicitly_wait(10) insted of :

time.sleep(10)

这篇关于如何在 Selenium 中关闭弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)