backpackerice backpackerice - 1 month ago 5x
Python Question

Python+Selenium, can't click the 'button' wrapped by span

I am new to selenium here. I am trying to use selenium to click a 'more' button to expand the review section everytime after refreshing the page.

The website is TripAdvisor. The logic of

button is, as long as you click on the first
button, it will automatically expand all the review sections for you. In other words, you just need to click on the first 'more' button.

All buttons have a similar class name. An example is like
. Only the numbers part changes everytime.

The full element look like this:

<span class="taLnk hvrIE6 tr413756996 moreLink ulBlueLinks" onclick=" var options = {
pid: 39415,
onSuccess: function() { ta.util.cookie.setPIDCookie(2247);'ta.servlet.Reviews.expandReviews', {type: 'dummy'},'review_413756996'), 'review_413756996', '1', 2247);; window.location.hash = 'review_413756996'; }
};'', {type: 'dummy'},'review_413756996'), options);
return false;
More&nbsp; </span>

I have tried several ways to get the button click. But since it is an onclick event wrapped by span, I can't successfully get it clicked.

My last version looks like this:

driver = webdriver.Firefox()
page_source = driver.page_source
soup = BeautifulSoup(page_source)
moreID = soup.find("span", class_=re.compile(r'.*\bmoreLink\b.*'))['class']
moreID = '.'.join(moreID[0:(len(moreID)+1)])
moreButton = 'span.' + moreID
button = driver.find_element_by_css_selector(moreButton)

However, I keep getting the error message like this:

WebDriverException: Message: Element is not clickable at point (318.5,
7.100006103515625). Other element would receive the click....

Can you advise me on how to fix the problem? Any help will be appreciated!


WebDriverException: Message: Element is not clickable at point (318.5, 7.100006103515625). Other element would receive the click....

This error to be occur when element is not in the view port and selenium couldn't click due to some other overlay element on it. In this case you should try one of these following solution :-

  • You can try using ActionChains to reach that element before click as below :-

    from selenium.webdriver.common.action_chains import ActionChains
    button = driver.find_element_by_css_selector(moreButton)
  • You can try using execute_script() to reach that element before click as :-

    driver.execute_script("arguments[0].scrollIntoView(true)", button)
  • You can try using JavaScript::click() with execute_script() but this JavaScript::click() defeats the purpose of the test. First because it doesn't generate all the events like a real click (focus, blur, mousedown, mouseup...) and second because it doesn't guarantee that a real user can interact with the element. But to get rid from this issues you can consider it as an alternate solution.

    driver.execute_script("arguments[0].click()", button)

Note:- Before using these options make sure you're trying to interact with correct element using with correct locator, otherwise would work well after wait until element visible and clickable using WebDriverWait.