【问题标题】:Failing to click on Dialog box continue option未能单击对话框继续选项
【发布时间】:2020-08-27 13:16:56
【问题描述】:

我有一个应用程序,它不断显示对话框消息以单击继续按钮 下面是继续会话的代码,它每分钟左右都会出现,然后一直让我烦恼。所以我可以在没有任何中断的情况下处理应用程序。所以,我写这个是为了自动化。它未能找到此元素,我也尝试了注释部分 #EC.alert_is_present() 和 #browser.switch_to().alert().accept() 这两个都不适合我。但是,它登录没有任何问题,但无法单击对话框继续按钮,该按钮不时出现。请帮我解决这个问题。

HTML:

<span class="dijitReset dijitInline dijitButtonText" id="confirmInforDialogActionOk_label" data-dojo-attach-point="containerNode">Continue</span>

代码试验:

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

browser = webdriver.Chrome(executable_path=r'D:\Jake\Python\chromedriver.exe')
browser.get('https://testtest.com/');
browser.maximize_window()
browser.find_element_by_name('j_username').send_keys('Jake')
browser.find_element_by_name('j_password').send_keys('Jake@123')
browser.find_element_by_xpath("//a[@class='btn btn-primary btn-block']").click()

App_loaded_one = WebDriverWait(browser,180).until (
EC.presence_of_element_located((By.ID, "userAvatarMenu"))
)

def alertpresent():  
    App_loaded = WebDriverWait(browser,300).until (
    #EC.alert_is_present()
    EC.presence_of_element_located((By.ID, 'onfirmInforDialogActionOk_label'))     
    )
    #browser.switch_to().alert().accept()  
    browser.find_elements_by_id('onfirmInforDialogActionOk_label').click()

    print ("Clicked dialog");
    print (datetime.now())

while True:
    alertpresent()

我在命令提示符下收到以下消息-

>  DevTools listening on
> ws://127.0.0.1:53745/devtools/browser/59aa8b6e-1dee-458e-afa5-653052f266aa
> Traceback (most recent call last):   File
> "C:\Users\Jake\Desktop\Instances\test.py", line 32, in <module>
>     alertpresent()   File "C:\Users\Jake\Desktop\Instances\test.py", line 22, in alertpresent
>     EC.presence_of_element_located((By.ID, 'onfirmInforDialogActionOk_label'))   File
> "C:\Users\Jake\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py",
> line 80, in until
>     raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

【问题讨论】:

  • 对话框中的对话框说什么?
  • 你能分享你的页面或页面链接的 HTML。你确定它是一个警报吗,我怀疑它可能是一个 iFrame。如果是这样,您需要切换到按钮所在的 Ifame,然后单击它并返回父框架。
  • 继续
  • @DebanjanB 您是在询问 HTML 源代码还是您要指出的其他内容。
  • @rahulrai 我敢肯定,即使我觉得它也不警觉,因为它看起来不像任何 javascript 警报。它的应用程序对话框要求我警告单击继续按钮以留在页面上。

标签: python-3.x selenium xpath css-selectors webdriverwait


【解决方案1】:

元素在Modal Dialog Box 内,因此要单击文本为Continue 而不是presence_of_element_located() 的元素,您必须将WebDriverWait 诱导为element_to_be_clickable(),您可以使用以下Locator Strategies:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.dijitReset.dijitInline.dijitButtonText#confirmInforDialogActionOk_label[data-dojo-attach-point='containerNode']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='dijitReset dijitInline dijitButtonText' and @id='confirmInforDialogActionOk_label'][@data-dojo-attach-point='containerNode']"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='dijitReset dijitInline dijitButtonText' and @id='confirmInforDialogActionOk_label'][@data -dojo-attach-point='containerNode']"))).click() --> 这对我有用。但是可能出现了第三次迭代异常,我认为我需要增加 webdriver 等待时间。我也可以在下面使用一个绝对xpath而不是xpath。 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "onfirmInforDialogActionOk_label")).click() 因为提供的 xpath 是绝对 xpath,可能会改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多