python selenium 等待页面加载_Python + 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()

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:

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

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?

Update:

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:

NU ÄR DU MEDLEM, Hello.

or

FORTSÄTT

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.

解决方案

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).

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

Waiter for the visibility of the text NU ÄR DU MEDLEM, Hello. :

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 :

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.')]")))

Waiter for the button with text FORTSÄTT :

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 :

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')]")))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用 PythonSelenium 进行自动化测试时,有时候需要等待页面元素加载完毕才能进行下一步操作,否则可能会出现找不到元素或者元素还没有加载完全的情况。下面介绍三种设置元素等待的方法。 1. 隐式等待 隐式等待是设置一个全局等待时间,当查找元素时如果未能立即找到则会等待一段时间,直到超时为止。这种等待方式在整个测试过程中都有效,比较方便。 示例代码: ```python from selenium import webdriver driver = webdriver.Chrome() driver.implicitly_wait(10) # 隐式等待10秒 # 接下来的操作,在元素加载完毕前最多等待10秒 ``` 2. 显式等待 显式等待是指在代码中指定等待条件,当满足条件时才进行下一步操作。这种等待方式更加灵活,可根据具体场景设置不同的等待条件,比如等待元素可见、可点击、出现在页面中等。 示例代码: ```python from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) # 显式等待10秒 # 等待元素可见 element = wait.until(EC.visibility_of_element_located((By.ID, 'myId'))) # 等待元素可点击 element = wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Click Me"]'))) ``` 3. sleep 等待 sleep 是最简单的等待方式,它会暂停当前线程指定的时间,不管页面元素是否已经加载完毕。这种等待方式不太灵活,如果页面元素加载时间较长,可能会导致等待时间不够,从而出现找不到元素的情况。 示例代码: ```python from selenium import webdriver import time driver = webdriver.Chrome() # 等待3秒 time.sleep(3) # 接下来的操作,在元素加载完毕前最多等待3秒 ``` 以上三种等待方式,根据实际情况选择合适的方式即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值