I'm attempting to retrieve text from an <a> tag with a ::before selector using selenium in python

<ul class="ipc-inline-list ipc-inline-list--show-dividers ipc-inline-list--inline ipc-metadata-list-item__list-content baseAlt" role="presentation">
                <li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm9297923/?ref_=tt_ov_st">Ashley Ballard</a></li>
                <li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm8484653/?ref_=tt_ov_st">Elizabeth Cascarelli</a></li>
                <li role="presentation" class="ipc-inline-list__item"><a class="ipc-metadata-list-item__list-content-item ipc-metadata-list-item__list-content-item--link" rel="" href="/name/nm8160673/?ref_=tt_ov_st">Jason Alan Cook</a></li>
            </ul>

https://i.stack.imgur.com/3BccU.png

I attempted various methods like XPATH and CSS selectors, but none have been successful.

actorname1 = WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 
'#__next > main > div > section.ipc-page-background.ipc-page-background--base.TitlePage__StyledPageBackground-wzlr49-0.dDUGgO > section > div:nth-child(4) > section > section > div.Hero__MediaContentContainer__Video-kvkd64-2.kmTkgc > div.Hero__ContentContainer-kvkd64-10.eaUohq > div.Hero__MetaContainer__Video-kvkd64-4.kNqsIK > div.PrincipalCredits__PrincipalCreditsPanelWideScreen-hdn81t-0.iGxbgr > ul > li.ipc-metadata-list__item.ipc-metadata-list-item--link > div > ul > li:nth-child(1) > a'))).text 
    

IT APPEARS UNATTRACTIVE

However, I encountered an error:

AttributeError: 'list' object has no attribute 'text'
.

Subsequently, I came across this solution on Python - get text from CSS property “content” on a ::before pseudo element in Selenium?

Yet, I struggle to grasp how to modify the code without knowledge of Javascript to resolve my issue.

Answer №1

If your selector is accurate, the output will be a list. Therefore, you should loop through it to access each item.

Remove the .text from the end of your element and iterate through it to extract the text values instead.

for actor in actorName1:
    print(actor.text)

Answer №2

actors= driver.find_elements_by_xpath("//a[@class[contains(.,'ipc-metadata-list-item')]]")
for index in range(len(actors)):
ActorName = actors[index].text

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm curious if it is possible to retrieve the dates of actions on websites using Selenium with Python

Is there a way to retrieve the date of data, like the date of comments, using HTML codes? I need assistance on how to achieve this through selenium in Python. Can Jquery be used as a solution? ...

Exception encountered for Internet Explorer when attempting to launch from WebDriver: 405 Method Not Allowed

After trying to launch IE from Webdriver and clicking on save, I encountered a "405 Method not allowed" exception. However, when I manually launched IE and clicked on save, it worked fine. I'm perplexed by this issue. Any ideas as to why it might be ...

Streamline your Salesforce application by automating drop down selections with XPath using Selenium WebDriver

https://i.stack.imgur.com/Eg9jc.pngAn application has been developed in Salesforce with a dropdown box containing items built using the <li> tag. However, I am unsure how to select a specific item from this design. <div id="Department__cformContr ...

Choosing a date from a jQuery datepicker-Ui with Selenium WebDriver

Is there a way for Selenium web driver to choose a specific date from a jQuery datepicker-UI and input it into a date field? 1) I attempted using Jscript to set the date, but it seems that the date must be chosen from the jQuery pop-up window for the form ...

A comparison between XPath and JQuery locators when using Selenium

After discovering that individuals utilize JQuery element-locators in Selenium, I am intrigued by the concept. I would like to inquire about the advantages of using JQuery selectors over XPath selectors. Are they considered more adaptable or quick, parti ...

Exploring the drag-and-drop feature in Ruby on Rails version 3

Is there a way to replicate drag and drop actions in rspec request specs? In my scenario, I need to move the #divItem element to the #divContainer element using jQuery. After refreshing the page, I want to verify that the #divItem is now inside the #divCo ...