我正在尝试使用Selenium在Google航班页面上找到目标输入框。该框本身没有任何有用的标识符(除非selenium可以使用jsname来查找元素)。现在,我使用一个定位器在两个c-wiz框中选择selenium,并得到以下错误:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string
我想我不太清楚如何使用相对定位器?我的最终目标是让Selenium将文本"JFK“发送到https://www.google.com/travel/flights上的目标输入框,因此如果有人有更优雅的方法,我会注意到:)
这是我的代码供参考:
bigWiz = bigBody.find_elements(wizLoc)"
正在抛出错误:
法典审判:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver_service = Service(executable_path = '/path/chromedriver')
url = 'https://www.google.com/travel/flights'
browser = webdriver.Chrome(service = driver_service)
browser.get(url)
bigBody = browser.find_element(By.ID,'yDmH0d')
wrongWiz = bigBody.find_element(By.ID, 'ow4')
wizLoc = browser.find_element(locate_with(By.TAG_NAME, 'c-wiz').below(wrongWiz))
bigWiz = bigBody.find_elements(wizLoc)
发布于 2022-08-21 18:57:47
谷歌航班页面上的目标输入框是一个动态元素。
若要向元素发送字符序列,需要为WebDriverWait导入https://stackoverflow.com/a/54194511/7429447,您可以使用以下任何一个https://stackoverflow.com/a/48056120/7429447
发布于 2022-08-21 21:04:37
您实际上可以在定位器中使用jsname
,例如,CSS选择器将是input[jsname='yrriRe']
。问题是它在页面上找到了8个元素,所以在这种情况下它是没用的。
你可以用几种方法。
活性元件
当页面第一次加载时,光标实际上位于您想要的元素中,因此您可以简单地使用
driver.switch_to.active_element.click()
driver.switch_to.active_element.send_keys("DFW")
注意:活动元素在单击后会发生变化,因此必须在第二行中重新获取它。
CSS选择器
您也可以使用CSS选择器来找到它。
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[aria-placeholder='Where to?'] input"))).send_keys("DFW")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-label='Enter your destination'] input"))).send_keys("DFW")
注意:与XPaths相比,您应该更喜欢CSS选择器,因为它们更受支持,速度更快,而且大多数情况下更容易阅读,因为语法更简单。
https://stackoverflow.com/questions/73437150
复制相似问题