Issue: I cannot get a clickable variable that points the chosen anime title. The title is an tag that has a tag that contains the anime name. What I want to do is: 1)Get all anime that appear from the website 2)Select the anime that has the same name as the input variable “b” 3)Get the chosen anime title clickable to redirect to its page and datascrape it.
What is causing me a lot of issues is the selection of the right anime, because all anime titles only share the same class name and the “presence of the strong tag” and that doesn’t seem enough to get the title clickable
This is the full program code for the moment: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time
a = input("Inserisci l'anime che cerchi: ") b = str(a) PATH = "C:Program Files (x86)chromedriver.exe" driver = webdriver.Chrome(PATH) driver.get("https://myanimelist.net/anime.php") print(driver.title) '''CHIUDIAMO LA FINESTRA DEI COOKIE, CHE NON MI PERMETTE DI PROSEGUIRE COL PROGRAMMA''' puls_cookie = driver.find_element(By.CLASS_NAME, "css-47sehv") puls_cookie.click() search = driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div[3]/div[2]/div[3]/form/div[1]/div[1]/input") time.sleep(2) search.click() search.send_keys(b) search.send_keys(Keys.RETURN) search2 = driver.find_elements(By.TAG_NAME, "strong") i = 0 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() time.sleep(10) driver.quit()
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
I AM VERY SORRY IF THIS EDIT DOESN’T STILL MAKE THE ISSUE CLEAR, english is not my native lenguage and I’m pretty new to programming too so its very difficult for me. I thank everyone that spent and (I wish) will spend time trying to help me; God bless you all
Advertisement
Answer
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
anime = driver.find_element(By.XPATH, "//div[@class='js-scrollfix-bottom-rel']/article/div/div[2]/div[1]/a[1][text()='Date A Live']") driver.get(anime.get_attribute("href"))
Since we will no longer be looking for an element instead of a list, we can search it directly in driver.get
without assigning it to a variable.
driver.get(driver.find_element(By.XPATH, "//div[@class='js-scrollfix-bottom-rel']/article/div/div[2]/div[1]/a[1][text()='Date A Live']").get_attribute("href"))
You can call the link in the ‘href’ element found in the anime result with the get_attribute method
When the element is searched before it is loaded, an error is received because the element is not found. For this, WebDriverWait is used, which is waiting for the element to be loaded.
anime = "Date A Live" driver.get(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//div[@class='js-scrollfix-bottom-rel']/article/div/div[2]/div[1]/a[1][text()='{anime}']"))).get_attribute("href")) driver.get("https://myanimelist.net/anime.php") searchBar = driver.find_element(By.XPATH, "//input[@id='q']") anime = "Date A Live" searchBar.send_keys(anime) searchBar.send_keys(Keys.ENTER) driver.get(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//table/tbody/tr/td[2]/div/a[strong='{anime}']"))).get_attribute("href"))