Unable to locate element and TimeoutException when

2020-05-09 17:11发布

问题:

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) Method 1: CSS Selector

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

leads to:

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

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) Method 3 : 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()

but was faced with TimeoutException

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)

This is the url of the page: https://play.google.com/store/apps/details?id=com.Daylight.EzLinkAndroid&hl=en_SG

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! :)

回答1:

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

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.

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



回答2:

As the element is JavaScript enabled element so to click() it you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@role='button']//span/span[text()='Show more']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC