简体   繁体   English

Selenium Python - 等待元素加载并点击它

[英]Selenium Python - Wait for Element to load and click on it

I've been trying to figure out how to wait for an element to be loaded and then execute the click function in python.我一直在试图弄清楚如何等待一个元素被加载,然后在 python 中执行 click 函数。

The website I am talking about is: https://tempail.com/我说的网站是: https : //tempail.com/

As soon as I received an e-mail, I want the script to click on it and then execute further tasks.我一收到电子邮件,就希望脚本点击它,然后执行进一步的任务。

I tried to solve this problem through the "Try/Except Function", but I always receive error messages.我试图通过“Try/Except Function”解决这个问题,但是我总是收到错误信息。

Source of the site: https://i.imgur.com/xMrJg5J.png网站来源: https : //i.imgur.com/xMrJg5J.png

The problem is that the site uses generated IDs I can't use in the find_element_by function.问题是该站点使用了我无法在find_element_by函数中使用的生成 ID。

This is what I've tried so far: https://i.imgur.com/zxGMVpf.png这是我到目前为止尝试过的: https : //i.imgur.com/zxGMVpf.png

With try, I wanted the script to wait until the site received the mail.通过尝试,我希望脚本等到站点收到邮件。 As soon as the mail is in the inbox, it should click the link/the mail and open it.一旦邮件进入收件箱,它应该点击链接/邮件并打开它。

Apart from that, I looked up for more solutions, but nothing really helped out, but with this code I always receive the following error:除此之外,我查找了更多解决方案,但没有任何帮助,但是使用此代码我总是收到以下错误:

Message: no such element: Unable to locate element: {"method":"css selector","selector":".epostalar ul li.mail a"}

Plus it doesn't even wait for the mail.此外,它甚至不等待邮件。

As soon as you receive an e-mail, to invoke click() on it you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies :收到电子邮件后,要在其上调用click() ,您必须诱导WebDriverWait以使元素可点击,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.mailler li.mail a"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='mailler']//li[contains(@class, 'mail')]//a"))).click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM