KaanDev
KaanDev

Reputation: 79

Python Selenium cant recognize the search result from google

hey i looked and tested over 2 to 3 hours couldnt find anything which seems to help me. i want that python types to google search bar something and copy the result.

<div id="resultStats">Ungefähr 4.440.000 Ergebnisse<nobr> 
#that resultstats the text i want to copy the text which is inside ><

Code:

test = webdriver.Chrome('.\chromedriver.exe')
test.get('https://www.google.de')
test.find_element_by_id('lst-ib').send_keys('test123123')
webdriver.ActionChains(test).send_keys(Keys.ENTER).perform()
test.find_element_by_xpath('//*[@id="resultStats"]/text()').send_keys(Keys.CONTROL + 'v')

print(test)

The Error:

DevTools listening on ws://127.0.0.1:12366/devtools/browser/cc7779e1-c661-4adc-91e4-067b256718db
Traceback (most recent call last):
  File "c:./niche.py", line 34, in <module>
    test.find_element_by_xpath('//*[@id="resultStats"]/text()').send_keys(Keys.CONTROL + 'v')
  File "C:.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "C:.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:.\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//*[@id="resultStats"]/text()" is: [object Text]. It should be an
element.
  (Session info: chrome=65.0.3325.181)
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.16299 x86_64)

Thanks in advance :D

Upvotes: 0

Views: 961

Answers (2)

Corey Goldberg
Corey Goldberg

Reputation: 60644

try using the id to locate the element:

driver.find_element_by_id('resultStats')

Here is a complete working example:

from selenium import webdriver

# create your webdriver
driver = webdriver.Chrome()

# open google
driver.get('http://www.google.com')

# locate the search field
search_box = driver.find_element_by_name('q')

# type "foo" into search field
search_box.send_keys('foo')

# execute a search
search_box.submit()

# locate the resultStats element
result_stats = driver.find_element_by_id('resultStats')

# print the text of resultStats
print(result_stats.text)

this will print:

About 27,000,000 results (0.39 seconds)

Upvotes: 2

Larry
Larry

Reputation: 226

You can find your string and print it as follows:

yourText = test.find_element_by_xpath('//*[@id="resultStats"]').text

print(yourText)

Upvotes: 0

Related Questions