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?
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