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).
根据您分享的 HTMLs,您可以为任一 WebElements 引入服务员,如下所示:
As per the HTMLs you have shared you can induce a waiter for either of the WebElements as follows :
等待文字的可见性NU ÄR DU MEDLEM, Hello.:
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')]")))
相关文章