`driver.find_element(By.XPATH, “xpath”)` 不起作用

huangapple go评论66阅读模式
英文:

driver.find_element(By.XPATH, "xpath") not working

问题

我正在尝试学习如何在Python中使用Selenium,但我遇到了一个问题,阻止我继续进行下去。

正如你可能知道的,Selenium的先前版本与最新版本相比具有不同的语法,我已经尝试了一切来使用我的代码填写表单,但什么都没有发生。我正在尝试从[https://demo.seleniumeasy.com/basic-first-form-demo.html]中找到XPATH元素,但无论我做什么,都无法将我的消息输入到消息字段中。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path="C:/Users/SnappFood/.cache/selenium/chromedriver/win32/110.0.5481.77/chromedriver.exe")

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(1000)
message_field = driver.find_element(By.XPATH, '//*[@id="user-message"]')
message_field.send_keys("Hello World")
show_message_button = driver.find_element(By.XPATH, '//*[@id="get-input"]/button')
show_message_button.click()

通过这段代码,我期望填写表单中的消息字段,并单击"SHOW MESSAGE"按钮以打印我的输入文本,但发生的情况是,我的代码只是打开了一个带有空字段的新Chrome网页。
我必须提到,PyCharm没有报错,代码运行时也没有出现错误。

如果你能帮助我理解我做错了什么,我将不胜感激。

英文:

I am trying to learn Selenium in Python and I have faced a problem which stopped me from processing.

As you might know, previous versions of Selenium have different syntax comparing the latest one, and I have tried everything to fill the form with my code but nothing happens. I am trying to find XPATH element from [https://demo.seleniumeasy.com/basic-first-form-demo.html] but whatever I do, I cannot type my message into the message field.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path="C:/Users/SnappFood/.cache/selenium/chromedriver/win32/110.0.5481.77/chromedriver.exe")


driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(1000)
message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")
show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()

By this code, I expect to fill the message field in the form and click the "SHOW MESSAGE" button to print my typed text, but what happens is that my code only opens a new Chrome webpage with empty field.
I have to mention that I don't get any errors by PyCharm and the code runs with no errors.

I would really appreciate if you help me through this to understand what I am doing wrong.

答案1

得分: 0

使用这个 xpath 会选择两个元素 //*[@id="user-message"],你需要让它变得唯一。尝试下面的 xpath,它会按预期工作。

message_field = driver.find_element(By.XPATH, '//input[@id="user-message"]')

浏览器快照:

`driver.find_element(By.XPATH, “xpath”)` 不起作用

英文:

with this xpath you have two elements //*[@id="user-message"] . you need to make it unique. Try below xpath this will work as expected.

message_field = driver.find_element(By.XPATH,'//input[@id="user-message"]')

browser snapshot:

`driver.find_element(By.XPATH, “xpath”)` 不起作用

答案2

得分: 0

你需要等待页面正确加载。

为此,最佳方法是使用WebDriverWait:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")

# 等待页面加载
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.find_element(By.XPATH, '//*[@id="user-message"]'))

message_field = driver.find_element(By.XPATH, '//*[@id="user-message"]')
message_field.send_keys("Hello World")

show_message_button = driver.find_element(By.XPATH, '//*[@id="get-input"]/button')
show_message_button.click()

经过测试,它正常工作。

如果您知道加载时间,也可以使用time.sleep()等待加载:

import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(5)

message_field = driver.find_element(By.XPATH, '//*[@id="user-message"]')
message_field.send_keys("Hello World")
time.sleep(5)

show_message_button = driver.find_element(By.XPATH, '//*[@id="get-input"]/button')
show_message_button.click()
time.sleep(5)
英文:

You need to wait for the page loads correctly.

For this, the best approach is using WebDriverWait:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")

# Wait for the page to load
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.find_element(By.XPATH, '//*[@id="user-message"]'))

message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")

show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()

Tested, it works fine.

Also you can use time.sleep() to wait the load if you know the loading times:

import time

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demo.seleniumeasy.com/basic-first-form-demo.html")
time.sleep(5)

message_field = driver.find_element(By.XPATH,'//*[@id="user-message"]')
message_field.send_keys("Hello World")
time.sleep(5)

show_message_button = driver.find_element(By.XPATH,'//*[@id="get-input"]/button')
show_message_button.click()
time.sleep(5)

huangapple
  • 本文由 发表于 2023年2月19日 23:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定