Home > Net >  Unable to find an element by selenium
Unable to find an element by selenium

Time:03-17

I'm writing a program to get FPS from the website: http://www.spacegoo.com/solar_system/

from selenium import webdriver

url = "http://www.spacegoo.com/solar_system/"
driver=webdriver.Chrome("./chromedriver")


driver.get(url)
tmp=driver.find_element_by_xpath('/html/body/div[1]/span')
tmp=tmp.get_attribute('innerHTML')
print(tmp)

and whenever I use "find_element_by_xpath" the browser would just crash and the error messaage is :

ttest.py:8: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver=webdriver.Chrome("./chromedriver")
ttest.py:16: DeprecationWarning: find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value=xpath) instead
  tmp = driver.find_element_by_xpath('/html/body/div[1]/span')
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.8) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "ttest.py", line 16, in <module>
    tmp = driver.find_element_by_xpath('/html/body/div[1]/span')
  File "/home/kevinhuang/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 521, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/home/kevinhuang/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1248, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/home/kevinhuang/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "/home/kevinhuang/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Runtime.evaluate threw exception: TypeError: Object.create is not a function
    at new CacheWithUUID (<anonymous>:94:24)
    at getPageCache (<anonymous>:222:18)
    at callFunction (<anonymous>:432:17)
    at <anonymous>:460:23
    at <anonymous>:461:3
  (Session info: chrome=99.0.4844.51)
Stacktrace:
#0 0x55bf3300b7d3 <unknown>
#1 0x55bf32d67688 <unknown>
#2 0x55bf32d715ea <unknown>
#3 0x55bf32d69e98 <unknown>
#4 0x55bf32d69b5d <unknown>
#5 0x55bf32d6a2c2 <unknown>
#6 0x55bf32d6a5dc <unknown>
#7 0x55bf32d9d93f <unknown>
#8 0x55bf32d9dde1 <unknown>
#9 0x55bf32dd0d74 <unknown>
#10 0x55bf32dbb6dd <unknown>
#11 0x55bf32dcea0c <unknown>
#12 0x55bf32dbb5a3 <unknown>
#13 0x55bf32d90ddc <unknown>
#14 0x55bf32d91de5 <unknown>
#15 0x55bf3303c49d <unknown>
#16 0x55bf3305560c <unknown>
#17 0x55bf3303e205 <unknown>
#18 0x55bf33055ee5 <unknown>
#19 0x55bf33032070 <unknown>
#20 0x55bf33071488 <unknown>
#21 0x55bf3307160c <unknown>
#22 0x55bf3308ac6d <unknown>
#23 0x7f9326a86609 <unknown>

I've checked urllib3 and chardet are not the reason why it crashed. I asked some of my colleagues, but no one know why it happened. So I post here to ask you guys how should I do to get the FPS?

CodePudding user response:

  1. Selenium 4 no more supports find_element_by_xpath and all other find_element_by methods. You should use find_element(By.XPATH,..)
    instead.
  2. You need to use Expected Conditions explicit waits here.
  3. Your locator should be improved

This should work better:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "http://www.spacegoo.com/solar_system/"

driver=webdriver.Chrome("./chromedriver")
wait = WebDriverWait(driver, 20)
driver.get(url)

tmp = wait.until(EC.visibility_of_element_located((By.ID, "fps"))).text
print(tmp)

CodePudding user response:

Your code is correct you just have to correct your xpath in order to fetch fps in your script. Here's a sample code for you:

url = "http://www.spacegoo.com/solar_system/"
driver=webdriver.Chrome("./chromedriver")
driver.get(url)
tmp=driver.find_element_by_xpath("//*[contains(@id,'fps')]")
#tmp=tmp.get_attribute('innerHTML')
print(tmp)
  • Related