Search code examples
pythonseleniumselenium-webdriverxpathselenium4

Writing a line of code with Selenium Webdriver in Python


I've been having trouble creating a function in python using selenium to click a button on a webpage.

I'm following a tutorial that is out of date. In the tutorial the code is:

driver.find_element_by_xpath('//div[@data-value="39455542968391"]')

Now, I know that selenium has changed how this is done but I am struggling to figure out the correct way of going about it.

driver.find_element(by=By.XPATH, value=('//button[@class="nice-select open"]').click()

this line gives the error:

unexpected EOF while parsing

and

driver.find_element("xpath", '//*[@class="nice-select open"].click()

this line gives the error:

name "EOL while scanning string literal

Solution

  • This format of code from :

    driver.find_element_by_xpath('//div[@data-value="39455542968391"]')
    

    In needs to be written as:

    driver.find_element(By.XPATH, "//button[@class='nice-select open']").click()
    

    or

    driver.find_element(By.XPATH, '//button[@class="nice-select open"]').click()
    

    You have to add the following imports :

    from selenium.webdriver.common.by import By