'How can I click all the links in a web page with selenium
I really don't know the reason why it doesn't work for me. I must click on the links that are greater than 9 and less than 54, that is, only those on the left side of this page https://www.tabladecalories.net/ If I print the links, it shows them to me without any problem, but if I click on it, it sends me an error.
tag_a = driver.find_elements(by=By.TAG_NAME,value="a")
cont = 0;
for link in tag_a:
if cont > 9 and cont < 54:
driver.find_element(By.LINK_TEXT, link.text).click()
time.sleep(4)
cont += 1
the error that I get is:
Traceback (most recent call last):
File "C:\Users\jluiso315\Desktop\informacionTablaDeAlimentos\main.py", line 34, in <module>
driver.find_element(By.LINK_TEXT, link.text).click()
File "C:\Users\jluiso315\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webelement.py", line 77, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Users\jluiso315\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webelement.py", line 710, in _execute
return self._parent.execute(command, params)
File "C:\Users\jluiso315\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "C:\Users\jluiso315\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=100.0.4896.75)
Stacktrace:
Backtrace:
Ordinal0 [0x00659943+2595139]
Ordinal0 [0x005EC9F1+2148849]
Ordinal0 [0x004E4528+1066280]
Ordinal0 [0x004E6E04+1076740]
Solution 1:[1]
What you should do is use a selenium recorder, like kathalon or selenium ide. You could get it in chrome web store.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CoderPerson |
