How do I locate text using xpath?

Question:

I have been asked to improve my questions. I’m autistic; so I’m unable to lie. I did research the internet and actually couldn’t remember where I’d been on the internet as I wrote below; but on this question I realised I could comb through my internet history; so will do a better job of listing the pages I visit before posting a question.

I posted a question in a comment and then immediately saw the answerer fixing the question; so I assumed that they hadn’t seen my comment and deleted it. I later saw that the answerer had actually responded to my comment; so I will get into the practise of not deleting anything going forward.

I will resume contributing to the site too. I am consumed with my projects at the moment.

I spent a solid eight hours banging my head against this problem, before I asked it as a question here. This was asked 14 hours after a previous question; so the automatic bot might have thought I was asking questions too frequently; so I will remember to wait days before asking further questions.

Original question below

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 _ov_ls_menu_sm" aria-label="Go to list: Inclusive" class="sc-1aecbe70-4 grpYsz">
        <svg >question before; but that was pertaining to rating a show, which has a completely different setup of HTML

enter image description here

Asked By: Cyrus
||

Answers:

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')]
Answered By: KunduK