Selenium NoSuchElementException with driver.find_element_by_xpath

2024/11/8 17:48:07

First question: How do I make python minimize chrome?

Second question: When getting to the end page using the next button how do I tell python to go on.. and not give me an error?

driver.get("https://www.google.com/#q=" + user_input)
for n in range(5):driver.find_element_by_xpath("""//*[@id="pnnext"]/span[2]""").click()time.sleep(2)posts2 = driver.find_elements_by_class_name("_Rm")for post2 in posts2:print(post2.text)text_file.write(post2.text + "\n")

After executing this code I get the following error:

Traceback (most recent call last):File "C:/Users/Skid/PycharmProjects/untitled/2nd.py", line 20, in <module>driver.find_element_by_xpath("""//*[@id="pnnext"]/span[2]""").click()File "C:\Users\Skid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 258, in find_element_by_xpathreturn self.find_element(by=By.XPATH, value=xpath)File "C:\Users\Skid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element{'using': by, 'value': value})['value']File "C:\Users\Skid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in executeself.error_handler.check_response(response)File "C:\Users\Skid\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 181, in check_responseraise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="pnnext"]/span[2]"}(Session info: chrome=47.0.2526.111)(Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 10.0 x86_64)
Answer

You get NoSuchElementException, the driver can't find your element. Change your xpath to this

driver.find_element_by_xpath("//*[@id='pnnext']/span[2]").click()

Without 3 quotation marks.

https://en.xdnf.cn/q/120596.html

Related Q&A

Traverse Non-Binary Tree [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Muscle alignment in python

I have a problem with printing my output from muscle aligning in python. My code is:from Bio.Align.Applications import MuscleCommandline from StringIO import StringIO from Bio import AlignIOdef align_v…

why does my function is returning data type None??: Python datatype None

This is my python code for printing an absolute number. My function is returning type None. I am not getting what I have done wrong. Please help me. def n(num):if num<0:return (num*-1)no = input(&qu…

np.sum Not returning total counts

I am trying this code but it does not return total count for zero[x][y], in this case it should return 5 but all it displays 255 five time. THIS CODE IS FOR CONNECTED COMPONENTS AND ZERO IS ONE COMPONE…

Python Printing Dictionary Key and Value side by side

I want a program that prints Key and Value side by side for the following code:This is a Dictionary:d = {M: [Name1, Name2, Name3], F: [Name1,Name2,Name3]}I want the a program that prints in the followi…

can i use rusts match in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Consolidating Elements of a List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

html textarea via python using post function

<div><textarea cols="25" rows="12" name="box" id="box" tabindex="2">TEXT GOES HERE </textarea></div><div><p class=&quo…

Fibonacci in Python - Simple solution [duplicate]

This question already has answers here:How to write the Fibonacci Sequence?(67 answers)Closed 6 years ago.n1 = 1 n2 = 1 n3 = n1 + n2 for i in range(10):n1 + n2print(n3)n1 = n2n2 = n3According to what …

snake game: snake colliding with itself

Hi I am currently writing a snake game code and I am nearly finished however I am having difficulty writing a code which will cause the game to end if the head of the snake collides with its body, I th…