我无法使用 Selenium send_key 在占位符中键入文本

I cant type text into placeholder using Selenium send_key

我无法将文本输入到重量、长度、宽度和高度的占位符中。

它不适用于 .send_keys()。它留在占位符中什么都不做!!

有人可以帮我解决吗?我想不通!!!

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from time import sleep
import pandas as pd


chrome_options = Options()
# chrome_options.add_argument("--headless")
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(ChromeDriverManager().install(),options=chrome_options)
driver.get("https://calculator.shipany.io/")
wait = WebDriverWait(driver, 5)

dropdownOrigin = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='booking-form']//select[.//option[contains(.,'Origin')]]")))
selectOrigin = Select(dropdownOrigin)
selectOrigin.select_by_visible_text("Hong Kong")

dropdownDestination = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='booking-form']//select[.//option[contains(.,'Destination')]]")))
selectDest = Select(dropdownDestination)
selectDest.select_by_visible_text("Hong Kong")

weight = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='i_form']/div/div[2]/div[3]/input[1]")))
weight.send_keys("1")

length = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='dimension']/input[2]")))
length.send_keys("1")

width = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='dimension']/input[3]")))
width.send_keys("1")

height = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='dimension']/input[6]")))
height.send_keys("1")

button = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='i_form']/div/div[2]/div[5]/button[1]")))
button.click()

它不起作用的原因是 style attribute.It 是说 display: none; 您需要将元素的样式更改为 display: block; 才能访问元素。

使用java脚本执行器改变元素的样式。

driver.get("https://calculator.shipany.io/")
wait=WebDriverWait(driver, 5)
dropdownOrigin =wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='booking-form']//select[.//option[contains(.,'Origin')]]")))
selectOrigin=Select(dropdownOrigin)
selectOrigin.select_by_visible_text("Hong Kong")

dropdownDestination =wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='booking-form']//select[.//option[contains(.,'Destination')]]")))
selectDest=Select(dropdownDestination)
selectDest.select_by_visible_text("Hong Kong")

weight=wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Weight(kg)*']/following::input[1]")))
driver.execute_script("arguments[0].style.display = 'block';",weight)
weight.send_keys("1")

length=wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='L(cm)']/following::input[1]")))
driver.execute_script("arguments[0].style.display = 'block';",length)
length.send_keys("1")

width=wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='W(cm)']/following::input[1]")))
driver.execute_script("arguments[0].style.display = 'block';",width)
width.send_keys("1")

Height=wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='H(cm)']/following::input[1]")))
driver.execute_script("arguments[0].style.display = 'block';",Height)
Height.send_keys("1")