【问题标题】:Unable to locate and edit the value of an input element Selenium - Python无法定位和编辑输入元素 Selenium 的值 - Python
【发布时间】:2020-01-26 15:48:33
【问题描述】:

我有以下几点:

<input data-qa-id="" lion-href-autocomplete="/merchant/assignable-user- 
autocomplete" class="input-medium input-large form-control tt-input" 
type="text" autocomplete="off" value="mercendes" spellcheck="false" 
dir="auto" style="position: relative; vertical-align: top; background-color: 
transparent;">

我想编辑值“mercendes”并将其更改为其他值。

到目前为止我尝试的是这样的:

browser.find_element_by_class_name("input-medium input-large form-control tt-input")

browser.find_element_by_css_selector("input.data-qa-id")

但它们都不起作用。

当然,一旦我抓住了元素,我希望我能做 send_keys()。但我的问题是我找不到元素。谢谢

【问题讨论】:

  • 请将 html 代码作为文本发布,而不是作为图像发布
  • 没用过python/selenium,但是,find_element_by_class_name 可能只需要一个类,你试过find_element_by_class_name('input-medium') 吗?
  • 另外,我想说find_element_by_css_selector() 如果用作find_element_by_css_selector('input[data-qa-id]=""') 会起作用(但不知道这是否会得到你需要的元素)

标签: python selenium


【解决方案1】:

您可以尝试使用 XPath 查找元素:

from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

如果您使用的是 chrome,您可以关注 this question 查找元素的 XPath。

【讨论】:

    【解决方案2】:

    browser.find_element_by_class_name() 只支持单个类名,不支持多个类名。使用以下 css 选择器。

    browser.find_element_by_css_selector("input.input-medium.input-large.form-control.tt-input")
    

    如果您正在寻找更独特的 CSS 选择器,请尝试。

    browser.find_element_by_css_selector("input.input-medium.input-large.form-control.tt-input[lion-href-autocomplete*='merchant']")
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2020-05-28
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多