Warm tip: This article is reproduced from serverfault.com, please click

Unable to store a function in a variable in Selenium

发布于 2020-11-28 12:42:03

Okay so I was writing my first selenium code which is:

browser.get('https://www.jetbrains.com/pycharm/')
browser.find_element_by_link_text('Download')
<selenium.webdriver.remote.webelement.WebElement (session="69f43bf4a5e98baf24ad3746dd01b514", element="0fa96685-d871-464f-aaaa-af02238a5f08")>

I wanted to simply go to the pycharm website and click on downloads, which worked fine.

But when I tried to store the previous function in a variable like so:

elem = browser.find_element_by_link_text('Download')

I got all these errors:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed
  (Session info: chrome=87.0.4280.66)

I have no idea what they mean or what ive done wrong. Help much appreciated. PS: I am using pycharm and selenium chromedriver if thats helpful.

Questioner
shemjay
Viewed
11
Abhishek Rai 2020-11-28 20:57:11

No issues in this.

from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
browser.get('https://www.jetbrains.com/pycharm/')
element = browser.find_element_by_link_text('Download')
element.click()