from selenium import webdriver
import time, random
import threading
url = 'https://npm.runkit.com/'
def e():
driver = webdriver.Chrome()
driver.get(url)
time.sleep(10)
driver.find_element_by_xpath('/html/body/div/div/div/div/div/div/div/div[3]/div/div/div/div[1]/div[1]/div[6]/div[1]/div/div/div/div[5]/div/pre/span/span').send_keys('a')
for i in range(1):
t = threading.Thread(target=e)
t.start()
图片:
我想做的是让脚本在这个对话框中写入,可以在其中写入一些内容,但由于此错误,我始终无法选择路径:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
有人可以帮我做吗?
看来你复制了错误的路径
请找到以下代码:
from selenium import webdriver
import time, random
import threading
import os
cwd = os.chdir('C:/Users/huzi95s/Desktop')
url = 'https://npm.runkit.com/'
def e():
driver = webdriver.Chrome()
driver.get(url)
time.sleep(10)
driver.find_element_by_xpath('//*[@id="react-container"]/div/div/div/div[1]/div/div/div[1]/div[2]/div[1]/div/div')
for i in range(1):
t = threading.Thread(target=e)
t.start()
这将为您提供如图所示的对话框:
技巧是复制 XPath 而不是完整的 Xpath。这将消除错误消息
要在所需字段内发送字符序列,您需要:
诱导 WebDriverWait 使所需的 框架可用并切换到它。
诱导 WebDriverWait 使所需的元素可点击。
您可以使用以下任一定位器策略:
driver.get("https://npm.runkit.com/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://runkit.com/e/iframe')]")))
time.sleep(10)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.CodeMirror")))
ActionChains(driver).move_to_element(element).click().send_keys("a").perform()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
浏览器快照:
您可以在以下位置找到一些关于 NoSuchElementException 的相关讨论:
您首先需要切换上下文并使用 javascript 发布代码,然后像往常一样进行操作。 下面的代码对我有用
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://npm.runkit.com/")
# Switch to the iframe
iframe = WebDriverWait(driver, 10).until(
EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@src='https://runkit.com/e/iframe-sha256-1ee7603751d0e1ca08ef01ce31128c89a5a9d24ef886f8050bcdeab4aa17d91f']"))
)
# Locate the CodeMirror editor's content container
code_mirror_content = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".CodeMirror .CodeMirror-code"))
)
# Clear existing content (if necessary)
driver.execute_script("arguments[0].innerHTML = '';", code_mirror_content)
# Enter new content
new_code = "print('Hello, World!')"
driver.execute_script("arguments[0].innerHTML = arguments[1];", code_mirror_content, new_code)
time.sleep(10)
driver.switch_to.default_content()
iframe url 可以通过使用下面获取源页面 url 来找到
# Get the HTML content of the page
html_content = driver.page_source
# Print the HTML content to the console
print(html_content)