I am trying to click on the Inclusive button within my account on the IMDB website as illustrated by the image further below.
I have tried various combinations of selenium xpath values and have googled various links to attempt to achieve this including this. My current python is below less logging onto the IMDB website to be able to effect this change.
driver.get('https://www.imdb.com/title/tt14772170/?ref_=nv_sr_srsg_0')
wait = WebDriverWait(driver, 20)
# click on arrow button in bottom right hand corner of image further below
# to call up pop-up window wth lists like Inclusive
xpath = "//button[@class='ipc-split-button__iconBtn']"
wait.until(EC.element_to_be_clickable((By.XPATH, xpath))).click()
try:
match_found = True
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']"
button1 = driver.find_elements(By.XPATH, xpath)
xpath = "//*[contains(text(), 'Inclusive')]"
button2 = driver.find_element(By.XPATH, xpath)
xpath = "//div[@class='sc-1aecbe70-0 dpbfLr']/div[@data-titleinlist='false']/*[contains(text(), 'Inclusive')]"
button3 = driver.find_element(By.XPATH, xpath)
driver.execute_script("arguments[0].click();", button3)
except selenium.common.exceptions.NoSuchElementException:
match_found = False
Buttons 1 and 2 work as I want illustrating that the actual xpath I need for button3 should work too. Buttons 1 and 2 will not be needed in the final code and are there only to illustrate that they at least work separately. An example of the HTML I’m trying to scrape is below:
<div class="sc-1aecbe70-0 dpbfLr">
<div role="button" tabindex="0" data-titleinlist="false" class="sc-1aecbe70-1 fkjUqe">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--add sc-1aecbe70-2 csaBFR" id="iconContext-add" viewBox="0 0 24 24" fill="currentColor" role="presentation">
<path d="M18 13h-5v5c0 .55-.45 1-1 1s-1-.45-1-1v-5H6c-.55 0-1-.45-1-1s.45-1 1-1h5V6c0-.55.45-1 1-1s1 .45 1 1v5h5c.55 0 1 .45 1 1s-.45 1-1 1z"></path>
</svg>
Inclusive</div>
<a href="/list/ls560615425/?ref_=tt_ov_ls_menu_sm" aria-label="Go to list: Inclusive" class="sc-1aecbe70-4 grpYsz">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--chevron-right" id="iconContext-chevron-right" viewBox="0 0 24 24" fill="currentColor" role="presentation">
<path fill="none" d="M0 0h24v24H0V0z"></path><path d="M9.29 6.71a.996.996 0 0 0 0 1.41L13.17 12l-3.88 3.88a.996.996 0 1 0 1.41 1.41l4.59-4.59a.996.996 0 0 0 0-1.41L10.7 6.7c-.38-.38-1.02-.38-1.41.01z"></path>
</svg>
</a>
</div>
The python currently produces no error message but jumps from button3 = driver.find_element(By.XPATH, xpath) to the except clause
I am using PyCharm 2022.2.2 Build #PC-222.4167.33 with Python 3.10.7 and chromedriver win32 105.0.5195.52
I asked a similar question before; but that was pertaining to rating a show, which has a completely different setup of HTML
>Solution :
Your xpath seems wrong. Use the below xpath to click on the Inclusive button
//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]
So your code will be like
xpath = "//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]"
button3 = driver.find_element(By.XPATH, xpath)
driver.execute_script("arguments[0].click();", button3)
Please provide enough wait to element to be visible or clickable.
you can use below xpath as well.
//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button'][contains(., 'Inclusive')]
You can use that as well.
//div[@class='sc-1aecbe70-0 dpbfLr']//div[@role='button' and @data-titleinlist='false'][contains(., 'Inclusive')]
