简体   繁体   中英

How to click on elements using selenium driver?

I have been trying to scrape a web page of bookmyshow site using selenium. When the page is loaded, 2 popups will come. In those two we have to click on the required buttons to close them. When i try to find those elements, I am getting error. I made the driver to load the page completely by using sleep(). But still, I am unable to do so.

The code is:

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)

The output I am getting is:

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)

The html content is:(may be loaded by 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>

How to do this?How to click on those two elements?

Before performing any action on any elements which are inside the iframe we have switch to respective frame like this.

# 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()

And once the required action performed on the element in side the iframe we have bring the control back to default content to perform action on main window like this.

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM