
我嘗試使用Xpath和單擊按鈕FullXpath,但仍然沒有運氣。
這是簡單的代碼:
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()
但它在控制臺中失敗并顯示此訊息:
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"}
請告訴我如何使用 selenium 自動執行此點擊。
uj5u.com熱心網友回復:
我建議使用By,WebDriverWait和expected_conditions代替.find_element_by_xpath。
單擊粘貼按鈕后,您將收到權限提示。請參閱下文以克服它。
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')
uj5u.com熱心網友回復:
這將起作用:
pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()
不要忘記在它之前添加一些等待/延遲以確保頁面完全加載。
uj5u.com熱心網友回復:
嘗試改用 CSS 選擇器:
element = w.find_element_by_css_selector('div[class*="MuiGrid-root"] > div[class="jss473"]').click()
您可以在此處找到有關 css 選擇器的所有檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362319.html
上一篇:找不到By.xpath定位的元素
