Home > other >  Selenium, issue getting ID of a generic tag and clicking
Selenium, issue getting ID of a generic tag and clicking

Time:11-16

I'm a newbie at programming and I recently started learning Python and its libraries. After completing a little project on BeautifulSoup, I wanted to reach the next level learning Selenium but I'm having some issues understanding how it works. My objective is to create a program that goes to the MyanimeList website and does the following things:

1)Open the browser (Chrome in my case) and get rid of the cookie window immediately (click ACCEPT immediately, to prevent the disruption of the next part of the program) 2)Go to the website searchbar and insert the anime name that I put in an input variable at the start of the program. 3)Press the keyboard ENTER button to start the search 4)Confront the results that I got with the anime name I inserted at the start 5)If an anime has the same name, press the button to open the page dedicated to that anime (and in the future datascrape that specific website page)

My issue is that the tag that contains the anime name is this (example of 1 anime result):

Date A Live

and this makes 2 problem arise: 1)Since I am not working with only one tag, I cannot use the attributes that only one anime uses (like href, id and rel, since they are used only for one anime) and the only one that are shared between the animes is the class tag. To understand which anime is the correct one, I search in the strong tag with the command

search2 = driver.find_elements(By.TAG_NAME, "strong")

2)My results of the driver.find_elements (of "strong" and the class) are not clickable and I cannot seem to find a way to make the anime that I want (the one with the "strong" tag that is the same as my input text) clickable.

Please tell me if what I am asking is not coherent/what I wrote is not understandable. I thank you in advance for the time I'm making you waste

Website I use selenium on: My actual program. Yes I know, its shit

I wanted to open the page by clicking the blue name of one of the anime that came up as result of the previus input on the searchbar

CodePudding user response:

Your code is not working because driver.find_elements() returns a list, even if it only finds one element. You are probably getting an error like: 'list' object has no attribute 'Click'

I think an easier way would be to find the element whose text matches your input string. The driver.find_element_by_xpath() method can do this. The If your input string is stored in the variable b you could do this with something like:

link = driver.find_element_by_xpath(f"// a[contains(text(),\{b})]") # the a represents the <a> tag and the be represents your input text
link.Click()

CodePudding user response:

You don't need to specify index when you use for in.

#wrong usage
for element in search2:
     anime = search2[i].text

#proper usage
f or element in search2:
     anime = element.text

if you want to go to the page of the first anime that comes up as a result of the search

you can use code like this

animes = driver.find_elements(By.XPATH, "//div[@class='js-scrollfix-bottom-rel']/article/div/div[2]/div[1]/a[1]")
driver.get(animes[0].get_attribute("href"))

You can call the link in the 'href' element found in the anime result with the get_attribute method

  • Related