如何使用 selenium 驱动程序单击元素?

2023-12-23

我一直在尝试使用 selenium 抓取 bookmyshow 网站的网页。页面加载后,会出现 2 个弹出窗口。在这两个中,我们必须单击所需的按钮来关闭它们。当我尝试找到这些元素时,出现错误。我让驱动程序使用 sleep() 完全加载页面。但我仍然无法这样做。

代码是:

from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from time import sleep
s = requests.session()
driver = webdriver.Chrome('F:\chromedriver')
driver.get("https://in.bookmyshow.com/booktickets/VMAX/2061")
sleep(40)
e2 = driver.find_element_by_class_name('wzrkPPwarp')
e2.click()
print("done")
e1 = driver.find_element_by_class_name('No thanks')
e1.click()

element = driver.find_element_by_class_name('__buytickets')
element.click()
element1 = driver.find_element_by_class_name('rem-seats')
print(element1.text)

我得到的输出是:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"wzrkPPwarp"}
  (Session info: chrome=64.0.3282.140)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.16299 x86_64)

html内容是:(可以通过javascript加载)

<div id="wzrk_wrapper"><style>.wzrk-overlay{background-color:rgba(0,0,0,.15);position:fixed;left:0;right:0;top:0;bottom:0;z-index:10000}.wzrk-hidden{display:none}.wzrk-alert{background-color:#fbfbfb;border:1px solid #f0f0f0;font-family: Arial,sans-serif;width:346px;padding:15px 15px 5px;border-radius:6px;text-align:center;position:fixed;right:20px;top:20px;margin-left:0;margin-top:0;overflow:hidden;z-index:99999}@media screen and (max-width:540px){.wzrk-alert{width:auto;margin:auto 0;top:35%;left:15px;right:15px}}.wzrk-alert-heading{color:#606060;font-size:20px;width:250px;animation:none;text-align:center;font-weight:700;margin:0 auto 10px;display:block}.wzrk-alert-body{color:#939393;font-size:14px;font-weight:300;animation:none;margin-bottom:10px}.wzrk-button-container button{background-color:#dbdbdb;color:#fff;border:none!important;box-shadow:none!important;min-width:130px;font-size:14px;animation:none;font-weight:100;border-radius:3px;padding:8px 10px;margin:0 7px;cursor:pointer}.wzrk-alert button:focus{outline:0}.wzrk-alert button:active{background:#5dc241}.wzrk-alert button.confirm{background-color:#f28046}.wzrk-button-container button.cancel{background-color:#dbdbdb;color:#7c7c7c}.wzrk-button-container button.cancel:hover{background-color:#d7d7d7}.wzrk-powered{font-family: Arial,sans-serif;font-size:9px;color:#777;animation:none;margin-top:12px;margin-right:2px;text-align:right}.wzrk-powered img{display:inline-block;animation:none;margin-bottom:-2px;-webkit-filter:contrast(10%);filter:grayscale(150%)}@-webkit-keyframes showWizAlert{0%{transform:scale(.7);-webkit-transform:scale(.7)}45%{transform:scale(1.05);-webkit-transform:scale(1.05)}80%{transform:scale(.95);-webkit-transform:scale(.95)}100%{transform:scale(1);-webkit-transform:scale(1)}}@-webkit-keyframes hideWizAlert{0%{transform:scale(1);-webkit-transform:scale(1)}100%{transform:scale(.5);-webkit-transform:scale(.5)}}.wiz-show-animate{animation:showWizAlert .3s}.wiz-hide-animate{animation:hideWizAlert .2s}</style><div class="wzrk-overlay"></div><div class="wzrk-alert wiz-show-animate"><div class="wzrk-alert-heading">Get Personalized Updates</div><div class="wzrk-alert-body">Get notified when we find something interesting that you'll love!</div><div class="wzrk-button-container"><button id="wzrk-cancel" class="No thanks">Not Now</button><button id="wzrk-confirm" class="Sign me Up!" style="background-color: rgb(192, 44, 58);">OK</button></div><div class="wzrk-powered"><a href="https://clevertap.com" target="_blank">Powered by <img src="https://d2r1yp2w7bby2u.cloudfront.net/js/ct_logo.svg" height="9" style="height:9px;"></a></div></div></div>

<div class="wzrkPPwarp" style="color:#eaeaea;background-color:#2f323b;"><a href="javascript:void(0);" onclick="parent.$WZRK_WR.closeIframe('1517903135','wizParDiv');" class="wzrkClose" style="background-color:#353535;color:#ffffff;">×</a><div id="contentDiv" style="width:100%;" class="wzrk"><div class="jsCT_CTA"><table cellpadding="0" cellspacing="0" border="0"><tbody><tr><td class="imgTd" style="background-color:#2f323b"><img src="https://in.bmscdn.com/mailers/images/160701promotion/kabali_footer_bms_logo.jpg" height="60" width="60"></td><td style="vertical-align:middle;"><div class="wzrkPPtitle" style="color:#eaeaea">Save INR 300* on movie tickets!</div><div class="wzrkPPdscr" style="color:#eaeaea">Book for your favorite one using your MobiKwik wallet &amp; get 50%* Supercash on your transaction amount!</div></td></tr></tbody></table></div></div></div>

如何做到这一点?如何单击这两个元素?


在对内部的任何元素执行任何操作之前iframe我们已经切换到各自的frame像这样。

# To switch to frame
driver.switch_to.frame(driver.find_element_by_id("wiz-iframe"))

# Clicking on the element inside the frame
e2 = driver.find_element_by_xpath("//div[@class='wzrkPPwarp']//a")
e2.click()

一旦对 iframe 内部的元素执行了所需的操作,我们就将控件带回到默认内容,以便在主窗口上执行操作,如下所示。

# Switching back to main content
driver.switch_to_default_content()

# Then only we can access elements
e3 = driver.find_element_by_xpath("//button[@class='No thanks']")
e3.click()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 selenium 驱动程序单击元素? 的相关文章

随机推荐

  • 删除navigationItem.titleView后恢复navigationItem.title

    我的应用程序有 2 个选项卡 tabA and tabB 根据我设置的位置navigationItem titleView of tabB to a UIImageView when viewWillAppear 叫做 默认行为tabB就是
  • Android:DOM vs SAX vs XMLPullParser 解析?

    我正在使用 SAX 解析器解析 XML 文档 我想知道与哪个合作更好更快DOM SAX解析器 or XMLPull解析器 这取决于你在做什么 如果你有非常大的文件 那么你应该使用 SAX 解析器 因为它会触发事件并释放它们 内存中不会存储任
  • 响应内容必须是实现 __toString() 的字符串或对象,给定“boolean”。在拉拉维尔

    响应内容必须是实现 toString 的字符串或对象 给定 boolean 这是我的代码 Route get user ip function Request request ip request gt ip return ip retur
  • 在 mac osx lion 中安装(构建)matplotlib

    我安装了 pynum 和 scipy 在带有 python 2 7 的 osx Lion 上 但是当我尝试构建 matplotlib 时 git clone https github com matplotlib matplotlib gi
  • 使用 class_eval 和 instance_eval 访问 Ruby 类变量

    我有以下内容 class Test a 10 def show a puts a a end class lt lt self b 40 def show b puts b b end end end 为什么以下工作有效 Test inst
  • 在 C# 中创建 iCal 文件

    我正在寻找一种在 c asp net 中生成 iCalendar 文件 ics 的好方法 我找到了一些资源 但缺少的一件事是他们对引用可打印fields 具有回车符和换行符的字段 例如 如果描述字段未正确编码 仅显示第一行 并且可能会损坏
  • (403) 您的项目无权访问此功能

    一直遵循有关如何插入网络属性的文档 以便我可以动态创建跟踪代码 目标是将我们的分析转移到 Google Analytics 中 并在分析下在他们自己的 Google 帐户上自动创建客户网站 经过我在网上找到的信息 似乎这个功能可能被列入白名
  • Redis启动后是空的,虽然有.rdb文件

    在我的 redis cli 中运行 shutdown save 后 等待 save 命令完成 跟随 redis 日志 并备份我的 redis rdb 文件 我尝试从头开始启动 redis 服务器 它已启动具有空数据库 dbsize 0 我能
  • POI 4 XPage - 文档生成只能运行一次

    好吧 说实话 标题并没有说出全部真相 我正在使用带有多个按钮 保存 关闭 编辑等 和一个执行 POI 操作的按钮的自定义控件 它生成一个 Word 文件 我在这里遇到一个问题 点击 POI 按钮后 我的其他按钮 以及 POI 按钮 将不再工
  • JavaScript 中的这个

    我在 javascript 中使用 this 时遇到了一个令人困惑的问题 我有一个方法 get data 它返回对象的一些成员变量 有时它会将对象本身返回给我 我不知道为什么 有人能解释一下这里发生了什么吗 function Feed It
  • JIRA:生成每个用户的时间报告?

    抱歉 如果 SO 不是最好的地方 但我在 JIRA 中启用了时间跟踪 并且希望能够为给定日期范围内的每个用户生成时间报告 我唯一拥有的时间跟踪报告选项非常有限 并且不能满足我的要求 是否可以通过标准功能或免费插件来实现 您可能想查看用于 J
  • JavaFX - 带箭头画线(画布)

    我将此代码重写为 JavaFX 时遇到问题 private final int ARR SIZE 8 void drawArrow Graphics g1 int x1 int y1 int x2 int y2 Graphics2D g G
  • 在 EF Core 中如何选择特定列并保存

    我有以下 SQL 表 ID INT Status NVARCHAR 50 FileContent XML 使用 EF Core 我想选择ID and Status列 但不加载 XML 列 由于 xml 数据可能很大 我不想将其加载到内存中
  • MacVim 的西班牙语拼写检查器?

    如何为 MacVim 添加西班牙语拼写检查器 我读到这是一个有用的link ftp ftp uniovi es pub vim runtime spell ghindex html 但显然它不起作用 我该怎么办 我不了解 MacVim 但是
  • 了解使用 LFSR 实现 CRC 生成的两种不同方法

    There are two ways of implementing CRC generation with linear feedback shift registers LFSR as shown in this figure The
  • Spring Boot 转换 Enum 忽略大小写

    我有一个 spring boot 应用程序 它暴露了Rest API https i stack imgur com DT1tp png 此 API 接受枚举列表batchStatus作为查询参数 这batchStatus用于根据状态过滤所
  • 过程式编程比面向对象编程有什么优势吗?

    编辑 早些时候 我问这个问题可能是一个框架很糟糕的问题 即何时使用 OOP 与何时使用过程编程 一些回复暗示我是在寻求帮助理解 OOP 相反 我经常使用 OOP 但想知道何时使用过程方法 从反馈来看 我认为存在相当强烈的共识 即 OOP 通
  • setjmp 和 longjmp - 通过示例进行理解

    我知道 setjmp 和 longjmp 的定义 setjmp 将环境存储在堆栈上下文中 另一个恢复 但我认为我的部分还存在一些缺乏理解的地方 有人可以在很好的例子的帮助下向我解释一下我如何保证 如何保存以及如何恢复吗 我看到jmp buf
  • JavaScript SQL 插入循环

    我试图向函数传递一个数组 该数组应该通过循环运行并为每个递增的 SQL 语句调用 db transaction function updateColorData colorArray for var i 0 i
  • 如何使用 selenium 驱动程序单击元素?

    我一直在尝试使用 selenium 抓取 bookmyshow 网站的网页 页面加载后 会出现 2 个弹出窗口 在这两个中 我们必须单击所需的按钮来关闭它们 当我尝试找到这些元素时 出现错误 我让驱动程序使用 sleep 完全加载页面 但我