简体   繁体   中英

How do I click a button using Selenium in Python?

I'm trying to crawl review data on an app from a Google Play website page using Python and Selenium. What I'm trying to do here is to click the "Full Review" button to see the entire text of each review.

But I keep running into these errors each time:

ElementNotInteractableException: Message: element not interactable

or

AttributeError: 'list' object has no attribute 'click'

or

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".LkLjZd ScJHi OzU4dc  "}

The element for the button is this:

<button class="LkLjZd ScJHi OzU4dc  " jsaction="click:TiglPc" jsname="gxjVle">Full Review</button>

The xpath for the button is this:

//*[@id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button

And this is the code I'm using for xpath:

full_review = driver.find_elements_by_xpath('//*[@id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/div/div[26]/div/div[2]/div[2]/span[1]/div/button')
full_review.click()

I can't find the button by class name, xpath or anything. Could anyone help me figure out this problem? In addition, is it possible to find the element by jsname? I would really appreciate if you could help.

Avoid using xpath whenever possible, it's the most brittle selector.

id > CSS > Xpath

For your button, this css selector should work:

button[jsname='gxjVle']

You'll probably need to specify the child as that probably won't be unique

  1. Your XPath selector is a little bit flaky, you can simplify it to be something like:

     //button[text()='Full Review'] 
  2. You're using the wrong function, if you're looking for a single unique element you should be using WebDriver.find_element_by_xpath one, mind that element , not elements
  3. It's better to use Explicit Wait just in case element is not immediately available in DOM (for example if you're testing application built using AJAX technology )

Assuming all above:

full_review = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Full Review']")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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