0

I am having an issue where after calling get(), the code sometimes works, other times it doesn't. No search results have this issue. Why would it work sometimes, other times it won't. Simple code to reproduce:

def get_url(search):
    options = Options()
    options.add_argument('-headless')
    driver = webdriver.Firefox()
    driver.get('https://genius.com/')
    print('go')
    elem = driver.find_element(By.ID, 'application')
    print(elem)
    driver.quit()

I've been troubleshooting for hours, so it's an excerpt at it's simplest, with some debugging. It could work twice in a row, then not work twice or three times. Rinse and repeat. Any ideas?

EDIT: Also, I have tried WebDriverWait as well. Same thing happens. Sometimes it succeeds, other times it doesn't. When it doesn't succeed, it seems to get stuck at get(). After 1 or 2 mins, it errors out and the script closes. Odd.

malonn
  • 59
  • 1
  • 7

1 Answers1

0

As per your code trials:

elem = driver.find_element(By.ID, 'application')

The locator strategy identifies the element:

<div id="application">...</div>

which is more or less the entire webpage

webpage

In realtime scenarios there would be seldom any usecase where where you would like to identify the enire application as a WebElement. Generally, WebElement are much smaller elements for e.g. an <input>, a <span>, a <div> etc.


This usecase

To print the DOM Tree entire application a better approach would be to print the page_source as follows:

print(driver.page_source)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Yeah, that was just troubleshooting code to get it to work. For me, the driver would sometimes find the element, sometimes not. Repeatedly. I just stepped back, looked at ToS, and said "no". The site's ToS does not allow scraping, and I can't get the scraper to work anyway, so I just dropped the project. Thanks though. – malonn Feb 16 '23 at 22:54
  • Doesn't this answer explains why the driver would sometimes find the element, sometimes not? – undetected Selenium Feb 16 '23 at 23:04
  • I don't know, TBH. I was having trouble with elements that were not the root element as well—same thing would happen. I just posted mid-troubleshooting. I wasn't actually trying to get the root element in my real project. It was a `
    – malonn Feb 17 '23 at 11:52