如何使用seleniumpython单击弹出窗口阻止的元素?

2024-04-25 09:19:56 发布

您现在位置:Python中文网/ 问答频道 /正文

website shows pop-ups just after logging in 我想单击左侧菜单中的“打开”选项,但它们被弹出窗口阻止。我该怎么做? 以下是我尝试过的:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
chromedriver = "/usr/share/chromedriver/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://student.amizone.net")
driver.find_element(By.NAME, "_UserName").send_keys("username")
driver.find_element(By.NAME, "_Password").send_keys("password")
driver.find_element(By.CSS_SELECTOR, "#loginform .login100-form-btn").click()
driver.implicitly_wait(10)
#11 | click | id=ModalPopAmityHostel |  
driver.find_element(By.ID, "ModalPopAmityHostel").click()
# 11 | click | id=StudentSatisfactionPop |  | 
driver.find_element(By.ID, "StudentSatisfactionPop").click()

此代码关闭第一个弹出窗口,但不关闭第二个弹出窗口。在代码中我首先登录到网站https://student.amizone.net我没有显示我的用户名和密码(显然)。之后driver.find_element(By.ID, "ModalPopAmityHostel").click()应该在关闭弹出窗口的弹出窗口外单击。类似地driver.find_element(By.ID, "StudentSatisfactionPop").click()应该关闭第二个弹出窗口。你知道吗

这是弹出元素的html代码片段:

<form action="/PopUp/Home/SANGATHANQuizpopupSave" data-ajax="true" data-ajax-loading="#lodingDiv" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-success=" $('#SANGATHANQuizpop').modal('hide');$('.modal-backdrop').remove();$(document.body).removeClass('modal-open');alertify.alert('Successfully Saved.'); " data-ajax-update="#Div_Partial" id="form0" method="post"></form> <div id="StudentSatisfactionPop" class="modal fade in" role="dialog" aria-hidden="false" style="display: block; padding-right: 15px;"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">STUDENT SATISFACTION SURVEY </h4> </div> <div class="modal-body"> <div> <p> <b>Dear Mr MANIK RAINA </b> </p> <p> Please tell us about you! Office of Research, Planning &amp; Statistical Services, Amity University Uttar Pradesh is conducting a survey of its students. This survey asks your opinion on many items relevant to examining the impact of college. It asks about your transition to university, your academic habits and experiences, your interaction with peers and faculty, your involvement in campus activities and programs, and how you spend your time. </p> <p>Results from this survey will be used by us to understand and improve your experiences. Data received from your responses will be analyzed and interpreted batch wise only, and at no time the responses be linked with an individual. Data will be used for revision and improvement of resource planning for better learning experiences of students of future batches. Be assured that your responses will remain confidential.</p> <p>We would very much appreciate you taking a few moments to fill out the Survey Questionnaire.</p> <p><b> To Start the survey : <a data-ajax="true" data-ajax-begin="$('#sidebar').removeClass('display');" data-ajax-loading="#lodingDiv" data-ajax-mode="replace" data-ajax-success=" $('#StudentSatisfactionPop').modal('hide');$('.modal-backdrop').remove();$(document.body).removeClass('modal-open');" data-ajax-update="#Div_Partial" href="/Survey/StudentSatisfaction" id="12" rel="0">Click Here</a>. </b></p> </div> </div> <div class="modal-footer"> <button type="button" id="btnClosevoter" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> <script> $(document).ready(function () { $('#StudentSatisfactionPop').modal('show'); }); </script> <script> $(document).ready(function () { $('#ModalPopAmityHostel').modal('show'); }); </script> <div id="ModalPopAmityHostel" class="modal fade in" role="dialog" aria-hidden="false" style="display: block;"> <div class="modal-dialog " style="z-index:104546464; "> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">Amity Hostel</h4> </div> <div class="modal-body"> <p class="text-center"> “A few hostel seats (A/C and Non A/C) are available for allocation. Students desirous of availing the seats may apply on Amizone and also contact hostel office. Allocation of seats will be done on ‘first come, first served’ basis.” </p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> <h4 class="modal-title"></h4> </div> </div> </div> </div>

Tags: andofdividyourdatabydriver
2条回答

由于文本为Close<button>标记在Modal Dialog Box中,因此要定位和click()所需元素,您必须为element_to_be_clickable()归纳WebDriverWait,并且可以使用以下Locator Strategies

  • 使用CSS_SELECTOR

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://student.amizone.net')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form#loginform input[name='_UserName']"))).send_keys("7071804")
    driver.find_element_by_css_selector("form#loginform input[name='_Password']").send_keys("62ae6f")
    driver.find_element_by_css_selector("form#loginform button[type='submit']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#ModalPopAmityHostel div.modal-footer button.btn.btn-primary[data-dismiss='modal']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#StudentSatisfactionPop div.modal-footer button.btn.btn-default[data-dismiss='modal']"))).click()
    
  • 使用XPATH

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://student.amizone.net')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[@id='loginform']//input[@name='_UserName']"))).send_keys("7071804")
    driver.find_element_by_xpath("//form[@id='loginform']//input[name='_Password']").send_keys("62ae6f")
    driver.find_element_by_xpath("//form[@id='loginform']//button[type='submit']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='ModalPopAmityHostel']//div[@class='modal-footer']//button[@class='btn btn-primary' and @data-dismiss='modal']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='StudentSatisfactionPop']//div[@class='modal-footer']//button[@class='btn btn-default' and @data-dismiss='modal']"))).click()
    

单击“关闭”按钮可以关闭弹出窗口。在代码中,单击div而不是close按钮,这就是弹出窗口不关闭的原因(请参阅close按钮的正确定位器)。你知道吗

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chromedriver = "/usr/share/chromedriver/chromedriver"
driver = webdriver.Chrome(chromedriver)
wait = WebDriverWait(driver, 10)

driver.get("https://student.amizone.net")

driver.find_element(By.NAME, "_UserName").send_keys("username")
driver.find_element(By.NAME, "_Password").send_keys("password")
driver.find_element(By.CSS_SELECTOR, "#loginform .login100-form-btn").click()

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ModalPopAmityHostel button.btn"))).click()
driver.execute_script("arguments[0].click()", driver.find_element_by_css_selector("#StudentSatisfactionPop button.btn"))
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#StudentSatisfactionPop button.btn"))).click()

相关问题 更多 >