Home > Back-end >  How to Fix: Unable to locate element: method- XPath
How to Fix: Unable to locate element: method- XPath

Time:11-19

enter image description here

I have tried clicking the button using Xpath and FullXpath, but still got no luck.

This is the simple code:

w = webdriver.Chrome(executable_path='chromedriver.exe',
                     chrome_options=options)

w.get("https://quillbot.com/")
time.sleep(5)
pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()

But it fails with this message in the console:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="inOutContainer"]/div[2]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/div/div/button/span[1]/div"}

Please show me how to automate this click using selenium.

CodePudding user response:

I recommend using By, WebDriverWait, and expected_conditions in the place of .find_element_by_xpath.

After you click the paste button you will receive a permissions prompt. See below to get past it.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.chrome.service import Service
import time
import pyautogui


service = Service('C:\\Path_To_Your\\chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get('https://quillbot.com/')

paste_button = WebDriverWait(driver, 3).until(EC.visibility_of_element_located(
            (By.XPATH, "//span[text()='Paste Text']")))

paste_button.click()
time.sleep(2)

pyautogui.press('tab')
pyautogui.press('tab')
pyautogui.press('enter')

CodePudding user response:

Try to use CSS selector instead:

element = w.find_element_by_css_selector('div[class*="MuiGrid-root"] > div[class="jss473"]').click()

You can find all the doc about css selector here

CodePudding user response:

This will work:

pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()

Don't forget to add some wait / delay before it to make sure the page is fully loaded.

  • Related