Python + Selenium:等待元素完全加载 [英] Python + Selenium: Wait until element is fully loaded

查看:1271
本文介绍了Python + Selenium:等待元素完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试使用Selenium中的函数:

So I have been trying to play around with the function in Selenium that is called:

wait = WebDriverWait(browser, 20).wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="accountStandalone"]/div/div/div[2]/div/div/div[1]/button')))
wait.click()

在我开始说这个问题之前.我要做的Selenium基本上是使Selenium自动写入这张图片中的小灵兽中:

Before I'm starting to say the issue. What I'm trying to do a Selenium of is to basically make a Selenium that automatic write to the forumlar in this picture:

没有什么并发症.但是,每当我按"Skapa Konto"时,它就会加载并等待直到出现新页面:

Which isn't any complications. However whenever I press "Skapa Konto", It loads and waits until a new page comes up which is:

上面是哪个图片.我的想法是,我希望它应该等到它给我图片"(该链接是相同的链接,这样它才能进行任何更改),因此我认为最好是等到文本等FORTSÄTT或HELLO"是浏览器.然后继续.

Which is the picture above. My idea is that what I wish is that it should wait until it gives me that "picture" (Which is the same link so it doesn't make any changes) so I assume better to do is to wait until a text etc "FORTSÄTT or HELLO" is the browser. Then continue.

但是,尝试使用此功能时出现问题.原因是它不等到找到它,而是立即运行并执行其他不应执行的操作.现在,它只是跳过等待,就像该功能不起作用或根本不存在一样.我做错了什么?

However, I'm having an issue when trying using this. The reason is that it doesn't wait until it found but goes instant and does other stuff which it shouldn't. Right now it just skips the wait like the function doesn't work or is there at all. What did I do for wrong?

更新:

我所知道的是,每当我尝试在该网站上注册时-该网站不会更改,这意味着当它成为一个成功的帐户后,它将带我进入一个新页面.但是它会自动刷新并说它是成功的.因此,这意味着我想以某种方式制作某种东西,使其能够检查并查看页面是否发生了新的变化.如果不是,请重试,然后重试?...类似的东西?

What I know is that whenever I try to register on the website - The website doesn't change meaning it takes me to a new page when its been a successful account. But it does automatic refresh and saying its been successful. So meaning that somehow I want to make something in a way that it checks and sees if something new happened to the page. If not, Wait again and try again?... Something like that?

我要做的是等检查是否存在:

What I would do is etc check if there is:

<div class="confirmation-title nsg-font-family--platform nsg-text--black edf-title-font-size--xlarge js-confirmationTitle">NU ÄR DU MEDLEM, Hello.</div>

<button type="button" class="nsg-button nsg-bg--black register-next-step-cta js-nextStepCta">FORTSÄTT</button>

然而,问题出在我所说的问题上,每当我按"SKAPA KONTO"时-它只等待服务器再次检查,然后自动刷新页面并说成功.

However the problem is as I said, whenever I press "SKAPA KONTO" - It just waits for the server to double check and then automatic refreshes the page and says successful.

推荐答案

首先,我坚信您已经很接近了.您只需要使用 Pythonic 格式化代码,就可以立即解决您的问题,如下所示:

First of all I strongly believe you were pretty close. You simply need to format your code in a Pythonic which may solve your issue straight away as follows :

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="accountStandalone"]/div/div/div[2]/div/div/div[1]/button'))).click()

您提到了它不会等到发现它之后立即发生,并且会执行其他不应该发生的事情,而不是提到您的程序应该做什么,从而使您对实际问题大加赞赏(例如您的代码试用版)以及您的程序在做什么错误(例如错误堆栈跟踪).

You have pulled a rug over the actual issue by mentioning it doesn't wait until it found but goes instant and does other stuff which it shouldn't rather than mentioning what your program is supposed to do (e.g. your code trials) and what wrong your program is doing (i.e. error stack trace).

根据您共享的 HTML ,您可以诱使以下任一 WebElements 服务员:

As per the HTMLs you have shared you can induce a waiter for either of the WebElements as follows :

  • 等待文本可见性的 NUÄRDU MEDLEM,您好.:

  • CSS_SELECTOR:

  • CSS_SELECTOR :

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.confirmation-title.nsg-font-family--platform.nsg-text--black.edf-title-font-size--xlarge.js-confirmationTitle")))

  • XPATH:

  • XPATH :

    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='confirmation-title nsg-font-family--platform nsg-text--black edf-title-font-size--xlarge js-confirmationTitle' and contains(.,'NU ÄR DU MEDLEM, Hello.')]")))
    

  • 等待带有文本FORTSÄTT的按钮:

    • CSS_SELECTOR:

    • CSS_SELECTOR :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.nsg-button.nsg-bg--black.register-next-step-cta.js-nextStepCta")))
    

  • XPATH:

  • XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='nsg-button nsg-bg--black register-next-step-cta js-nextStepCta' and contains(.,'FORTSÄTT')]")))
    

  • 这篇关于Python + Selenium:等待元素完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆