1

I'm trying to create a script that should download a given episode. For example, I want to open the magnet of episode 6 in 720p. However, Selenium doesn't find the element even though the id is correct.

I first tried using Xpath, but I get the same error.

Attempt

def downloadEpisode(episode):
    if episode > 9:
        id = str(episode)
    else:
        id = "0" + str(episode)
    #xpath = "//*[@id='" + id + "-720p']/span[2]/a" copied it using element inspect
    driver = webdriver.Chrome()
    driver.get("https://horriblesubs.info/shows/black-clover/")

    element1=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, id)))

    #also tried these two below
    #element1 = driver.find_element_by_id(id)
    #element1 = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,xpath)))

    element2 = element1.find_element_by_id(id + "-720p")
    element3 = element2.find_element_by_link_text("magnet")

    actions = ActionChains(driver)
    actions.click(element3).perform()

How do I solve this problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
metalbea
  • 43
  • 1
  • 7

1 Answers1

0

It sounds like your encountering a very common, but completely resolvable issue. You first need to scroll into view (like scrolling down a page) before attempting to perform the click action on the element, so it then becomes visible to the DOM. Either with actionchains or execute_script.

actions.move_to_element(element).perform()

This older post should make things a little clearer buddy :) Scrolling to element using webdriver?

David Silveiro
  • 1,515
  • 1
  • 15
  • 23
  • Ooh wow okay, I'll try it asap and let you know if it fixed my problem. But doesn't that mean that I have to let selenium first click on every show more first until there aren't anymore to be sure that it'll find the element? – metalbea Apr 01 '19 at 08:27
  • Just had a quick look over on the site and you're correct :) But that's only because you're using Selenium alone, and not with scrapy + BeautifulSoup. Those libraries are definitely worth looking into when you've got some time – David Silveiro Apr 01 '19 at 17:45
  • Could you mark the question as answered if you found my post helpful :) – David Silveiro Apr 02 '19 at 23:29
  • You're right, scrapy is what i need for what I want to achieve. Thank you very much! Sorry for late mark/response, I had no time to check it earlier. – metalbea Apr 05 '19 at 14:29