最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Selenium + XPath: element not found - Stack Overflow

matteradmin4PV0评论

I have a Selenium + Python + Chromedriver script which should log in to a website and click on a download button which will download a CSV file.

Inspect details of download button is:

<button id="csv-button" class="block tiny-margin-top" data-args="csv">
      CSV
</button>

and XPath is:

//*[@id="csv-button"]

but it says XPath element not found when I run the script. Please find the code below:

click_button = driver.find_element_by_xpath('//*[@id="csv-button"]')
click_button.click()

I have a Selenium + Python + Chromedriver script which should log in to a website and click on a download button which will download a CSV file.

Inspect details of download button is:

<button id="csv-button" class="block tiny-margin-top" data-args="csv">
      CSV
</button>

and XPath is:

//*[@id="csv-button"]

but it says XPath element not found when I run the script. Please find the code below:

click_button = driver.find_element_by_xpath('//*[@id="csv-button"]')
click_button.click()
Share Improve this question edited Mar 21, 2018 at 9:49 Ratmir Asanov 6,4695 gold badges29 silver badges43 bronze badges asked Mar 21, 2018 at 9:41 gosatrianigosatriani 3074 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Incase of a unique xpath identifying the WebElement if you are seeing element not found exception you need to induce WebDriverWait in conjunction with expected_conditions clause set to element_to_be_clickable as follows :

from selenium.webdriver.mon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other code    
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='csv-button']"))).click()

To be more granular you can use :

from selenium.webdriver.mon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other code    
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='block tiny-margin-top' and @id='csv-button']"))).click()

The problem seems that your xpath is not quite correct. You are using single quotes where double are required and visa versa.

Try:

click_button = driver.find_element_by_xpath("//*[@id='csv-button']")
click_button.click()

You can even try:

//button[@id='csv-button']

This will make the xpath search faster as it will look only for the button tags.

single quotes should e in the double quotes. Please see below

click_button = driver.find_element_by_xpath("//*[@id='csv-button']")

click_button.click()

see the change here:

('//*[@id="csv-button"]')   ---> ("//*[@id='csv-button']")
Post a comment

comment list (0)

  1. No comments so far