使用WebDriverWait时无法找到元素和TimeoutException [英] Unable to locate element and TimeoutException when WebDriverWait is used

查看:680
本文介绍了使用WebDriverWait时无法找到元素和TimeoutException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动单击页面底部的"SHOW MORE"按钮,以获取所有评论.

I'm trying to automate the clicking of the "SHOW MORE" button at the bottom of the page to get all the reviews there is.

但是,我在查找它时遇到了一些问题,如果您能帮助我,我将非常感谢.

However, I'm having some problems locating it and would really appreciate it if you could help me out.

我尝试了几种方法,但是我不确定为什么它们都不起作用.

I have tried a couple of methods but I am not sure why none of them work.

1)方法1:CSS选择器

1) Method 1: CSS Selector

driver.find_element_by_css_selector("U26fgb.O0WRkf.oG5Srb.C0oVfc.n9lfJ.M9Bg4d")

导致:

NoSuchElementException: Message: no such element: Unable to locate element

2)方法2:XPath Helper(Chrome上的扩展程序)

2) Method 2 : XPath Helper (an extension on Chrome)

driver.find_element_by_xpath("/html/body[@id='yDmH0d']/div[@id='fcxH9b']/div[@class='WpDbMd']/c-wiz[@class='zQTmif SSPGKf I3xX3c drrice']/div[@class='T4LgNb']/div[@class='ZfcPIb']/div[@class='UTg3hd']/div[@class='JNury Ekdcne']/div[@class='LXrl4c']/div/div[@class='W4P4ne ']/div[2]/div[@class='PFAhAf']/div[@class='U26fgb O0WRkf oG5Srb C0oVfc n9lfJ']/span[@class='CwaK9']/span[@class='RveJvd snByac']")

导致与上述相同的错误.

leads to the same error as above.

3)方法3:WebDriverWait

3) Method 3 : WebDriverWait

我阅读了与此相关的其他堆栈溢出问题,并尝试使用WebDriverWait,这是我的代码:

I read the other stack overflow questions related to this and tried using WebDriverWait and here is my code:

WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "/html/body[@id='yDmH0d']/div[@id='fcxH9b']/div[@class='WpDbMd']/c-wiz[@class='zQTmif SSPGKf  I3xX3c drrice']/div[@class='T4LgNb']/div[@class='ZfcPIb']/div[@class='UTg3hd']/div[@class='JNury Ekdcne']/div[@class='LXrl4c']/div/div[@class='W4P4ne ']/div[2]/div[@class='zc7KVe']/div[@class='d15Mdf bAhLNe']/div[@class='xKpxId zc7KVe']/div[@class='bAhLNe kx8XBd']/span[@class='X43Kjb']"))).click()

但遇到TimeoutException

but was faced with TimeoutException

4)当遇到此类错误时,我遇到了另一个有关更改框架的问题,但似乎我没有框架可以切换(如果我错了,请纠正我)

4) I came across another question on changing frames when faced with such errors but it seems like I don't have a frame to switch to (Do correct me if I'm wrong)

这是页面的网址: https://play.google.com/store/apps/details?id=com.Daylight.EzLinkAndroid&hl=zh_CN

我遇到问题的HTML如下:

The HTML that I have problems with is as follows:

<div class="PFAhAf" jscontroller="XO1Ihd" jsaction="JIbuQc:bRsdTc(i3y3Ic);">
      <div role="button" class="U26fgb O0WRkf oG5Srb C0oVfc n9lfJ M9Bg4d 
         j7nIZb" jscontroller="VXdfxd" jsaction="click:cOuCgd; 
         mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; 
         mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; 
         contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; 
         touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); 
         touchcancel:JMtRjd;j9grLe:.CLIENT;HUObcd:.CLIENT" jsshadow="" 
         jsname="i3y3Ic" aria-disabled="false" tabindex="0">
               <div class="Vwe4Vb MbhUzd" jsname="ksKsZd" style="top: 17.2px; 
                     left: 70.225px; width: 98px; height: 98px;"></div>
               <div class="ZFr60d CeoRYc"></div><span jsslot="" class="CwaK9"> 
             <span class="RveJvd snByac">Show more</span>
             </span>
      </div>
</div>

很长的帖子,很抱歉,谢谢您的帮助! :)

Sorry for the long post and thank you for your help! :)

推荐答案

您的yDmH0dfcxH9b等似乎是动态生成的,并且每次加载/重新加载页面时都会更改.唯一不变的是 span 标签文本.

Your yDmH0d, fcxH9b, etc. seems to be dynamically generated and change each time you load/reload the page. The only thing which doesn't change is span tag text.

所以我建议使用以下简单选择器:

So I would recommend using the following simple selector:

WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Show more']"))).click()

也可以考虑使用页面对象模型设计模式,它可以使您的生活变得更轻松,因为它可以在UI更改时/在其中更改UI,并使您可以更快地编写测试.

Also consider using Page Object Model design pattern, it will make your life easier when it comes to test support when/where UI changes and lets you write tests much faster.

请参见页面对象页面/selenium-python.readthedocs.io"rel =" nofollow noreferrer> Selenium Python文档,以在需要时提供更多信息.

See Page Objects page of Selenium Python documentation for more information if needed.

这篇关于使用WebDriverWait时无法找到元素和TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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