2

I work with Python and Selenium. The mission is to click on button with text '+like' or on column 'td' with class='profile-image'. But button hasn't id and it's class 'more-likes' is used in others buttons. Same situation with div with class 'profile-image-button' (div's class id used in others 'divs'). I tried to take id of 'td' :

button = photos.find('td', class_='profile-image')
print(button.get_id)

Output is 'None'

Here is html code of webpage:

<div id="category7515692" class="category-content" data-content="present" data-collapsing="true">
  <table class="pictures" data-columns-count="12" data-type="gallery">
    <tbody class="" data-selec="7565904" data-name="beauty" data-live="true">
      <tr data-mutable-id="MR1main" class="header">
        <td class="main-row-buttons" rowspan="1" data-mutable-id="Bmain">
          <table>
            <tbody>
              <tr>
                <td class="profile-image" id="view-75634" data-event-more-view="event-more-view" data-selec="7565904" islive="true" isseparatedbutton="false">
                  <div class="profile-image-button">
                    <span class="more-likes">+like</span>
                  </div>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
</div>

How can I click on button or how can I take id?

Boris Veselov
  • 75
  • 1
  • 8

2 Answers2

0

Assuming, there is only one button with text '+like', you can search an element with specific text like this:

driver.find_element_by_xpath("//*[contains(text(), '+like')]").click()
Kamal
  • 2,384
  • 1
  • 13
  • 25
0

The desired element is an React element so to click the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td.profile-image>div.profile-image-button>span.more-likes"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='profile-image']//span[@class='more-likes' and contains(.,'+like')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for an answer! I heard something about mechanicalsoup. Could it be the solution? Sorry, I've never worked with it... – Boris Veselov Dec 04 '18 at 10:24
  • Not aware about _mechanicalsoup_, did you try out the solution? Can I have the status please? – undetected Selenium Dec 04 '18 at 10:25
  • 1
    OH MY GOD! It works! Thank you so much! But it clicks only on one button.I think we identify only 1 button with those parameters. But Webpage has at least 3 buttons. What I should do if I need to click every button? (The thing is... when i click on button, it shows more information that I need to parse too) – Boris Veselov Dec 04 '18 at 11:14
  • You gotta raise a new question with your new requirement and StackOverflow contributors will be happy to help you out. – undetected Selenium Dec 04 '18 at 11:15
  • 1
    Okay. Thank you! – Boris Veselov Dec 04 '18 at 11:17