Search code examples
pythonparsingselenium-webdriverxpathselenium4

trying to click a html button with selenium with python


Trying to go trough a website using selenium to parse some stuff, but i cant click it on a button to load a pop up java script page

using firefox + geckowebdriver (latest)


view_larger_image_button = driver.find_element_by_xpath('//span[text()="View larger image"]')
driver.click(view_larger_image_button)

Here is the button :


<div class="main-link" data-spm-anchor-id="a2700.details.0.i4.2b7b5fc5f54UAP"><i class="detail-next-icon detail-next-icon-search detail-next-small"></i><span>View larger image</span></div>

i get the following error:

  File "image.py", line 17, in <module>
    view_larger_image_button = driver.find_element_by_xpath('//span[text()="View larger image"]')
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

Solution

  • All the find_element_by_* and find_elements_by_* methods are deprecated in current Selenium versions. You need to use driver.find_element(By.CLASS_NAME, " "), driver.find_element(By.XPATH, " ") etc. methods.
    So, instead of driver.find_element_by_xpath('//span[text()="View larger image"]') you should use

    driver.find_element(By.XPATH, '//span[text()="View larger image"]')
    

    You will also need this import:

    from selenium.webdriver.common.by import By