Selenium Web Driver – Send Keys 在 Chrome 上不起作用(xpath)

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

Selenium Web Driver - Send Keys Not working for Chrome (xpath)

问题

I can provide you with the translated code portion. Here it is:

来做一个简单的网络浏览器自动化并尝试输入登录信息到 Instacart

程序运行但难以让 send_keys 输入文本我不确定我的 XPath 是否不正确或者 send_keys 不起作用

浏览器打开并成功点击登录按钮但没有文本发送到电子邮件输入字段

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys

options = Options()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

# 转到 Instacart 网站
driver.get('https://www.instacart.com')

# 等待页面加载
driver.implicitly_wait(10)

# 使用 XPath 找到登录按钮
login_button = driver.find_element(By.XPATH, "//*[@id='react-root']/div/header/div[2]/nav/div[2]/button[1]/span")

# 点击登录按钮
login_button.click()

# 等待页面加载
driver.implicitly_wait(10)

# 找到电子邮件输入字段并输入文本
email_input = driver.find_element(By.XPATH, "//*[@id='id-82lr01']").send_keys("johndoe@gmail.com")

This is the translated code part without any additional content.

英文:

Doing a simple web browser automation and attempting to enter login information to instacart.

Program runs but having difficulty getting send_keys to input text. I'm not sure if my Xpath is incorrect or if the send_keys is not working.

The browser opens, and successfully clicks the login button. No text gets sent to the email input field.


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys


options = Options()
options.add_experimental_option("detach",True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)



# navigate to the Instacart website
driver.get('https://www.instacart.com')

# wait for the page to load
driver.implicitly_wait(10)

# find the login button using XPath
login_button = driver.find_element(By.XPATH, "//*[@id='react-root']/div/header/div[2]/nav/div[2]/button[1]/span")

# click on the login button
login_button.click()

# wait for the page to load
driver.implicitly_wait(10)

email_input = driver.find_element(By.XPATH, "//*[@id='id-82lr01']").send_keys("johndoe@gmail.com")


答案1

得分: 1

Here is the translated content:

"由于电子邮件输入框的ID每次页面刷新时都会自动生成,您需要使用另一种方法来获取它,这个CSS选择器应该有效:

email_input = driver.find_element(By.CSS_SELECTOR, "input[name='email'][placeholder='Email']").send_keys("johndoe@gmail.com")

此外,在每次点击之前不应使用 driver.implicitly_wait(10),在这种情况下,您应该在脚本的开头使用显式等待来获取元素,或者只在脚本开始时使用隐式等待一次。"

英文:

Since the id of the email input gets generated automatically each time the page gets refreshed, you need to use another way to get it, this CSS Selector should work:

email_input = driver.find_element(By.CSS_SELECTOR, "input[name='email'][placeholder='Email']").send_keys("johndoe@gmail.com")

Also, you should not use driver.implicitly_wait(10) before each click, in that case you should use Explicit Waits when getting the elements or just use the implicit wait once at the beginning of the script.

答案2

得分: 0

以下是翻译好的部分:

检查以下代码:

driver.get('https://www.instacart.com')
# 设置驱动程序实例的超时时间为10秒
driver.implicitly_wait(10)
# 使用XPath查找登录按钮
login_button = driver.find_element(By.XPATH, "(//span[text()='Log in'])[1]")
# 点击登录按钮
login_button.click()
# 在输入框中输入电子邮件
driver.find_element(By.XPATH, "//input[@placeholder='Email']").send_keys("johndoe@gmail.com")

问题的根本原因,正如其他回答中所指出的,是@id属性中的动态值。如果您首选的定位器是XPATH,您可以改用以下XPATH:

//input[@placeholder='Email']

以下是建议/改进

  1. 将下面的XPATH表达式:
//*[@id='react-root']/div/header/div[2]/nav/div[2]/button[1]/span

改为以下内容,这更易于阅读:

(//span[text()='Log in'])[1]
  1. 在下面的代码中,您不需要将任何内容存储在变量中,因为send_keys()不会返回任何内容。
email_input = driver.find_element(By.XPATH, "//*[@id='id-82lr01']").send_keys("johndoe@gmail.com")

所以将其改为:

driver.find_element(By.XPATH, "//input[@placeholder='Email']").send_keys("johndoe@gmail.com")
英文:

Check the below code:

driver.get('https://www.instacart.com')
# set timeout of 10s across driver instance
driver.implicitly_wait(10)
# find the login button using XPath
login_button = driver.find_element(By.XPATH, "(//span[text()='Log in'])[1]")
# click on the login button
login_button.click()
# Enter email in the input box
driver.find_element(By.XPATH, "//input[@placeholder='Email']").send_keys("johndoe@gmail.com")

Root cause, of your issue was as rightly pointed out in the other answer, dynamic value in the @id attribute. If your preferred locator is XPATH, you can use following XPATH instead:

//input[@placeholder='Email']

Suggestions/improvements below:

  1. XPATH expressions below:
//*[@id='react-root']/div/header/div[2]/nav/div[2]/button[1]/span

Change to below, this is more user readable.

(//span[text()='Log in'])[1]
  1. You don't need to store anything in a variable in below code as send_keys() won't return anything.
email_input = driver.find_element(By.XPATH, "//*[@id='id-82lr01']").send_keys("johndoe@gmail.com")

So change it to:

driver.find_element(By.XPATH, "//input[@placeholder='Email']").send_keys("johndoe@gmail.com")

答案3

得分: 0

以下是一个完整的SeleniumBase脚本,可以处理登录过程。

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class InstacartTest(BaseCase):
    def test_instacart(self):
        self.open("https://www.instacart.com/")
        self.click_if_visible("div button span svg path")  # 随机弹出窗口
        self.click('nav button span:contains("Log in")')
        self.type('input[name="email"]', "johndoe@gmail.com")
        self.type('input[name="password"]', "PASSWORD")
        self.sleep(5)

需要注意的几点是::contains()是一个独特的SeleniumBase选择器,在使用前会转换为xpath。该网站上有许多生成的ID选择器,因此最好使用其他属性,如name

英文:

Here's a complete SeleniumBase script that can handle that login process.

pip install seleniumbase, and then run with python:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class InstacartTest(BaseCase):
    def test_instacart(self):
        self.open("https://www.instacart.com/")
        self.click_if_visible("div button span svg path")  # Random pop-up
        self.click('nav button span:contains("Log in")')
        self.type('input[name="email"]', "johndoe@gmail.com")
        self.type('input[name="password"]', "PASSWORD")
        self.sleep(5)

A few things to note: :contains() is a unique seleniumbase selector that gets converted to xpath before use. A lot of selectors on that site have generated IDs, so it's best to use other attributes, such as name.

huangapple
  • 本文由 发表于 2023年4月17日 15:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032672.html
匿名

发表评论

匿名网友

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

确定