【问题标题】:python selenium click at current positionpython selenium 点击当前位置
【发布时间】:2020-12-01 17:11:01
【问题描述】:

我有一个下拉菜单,我需要单击它来激活。下拉列表的元素没有暴露在html中,因此无法找到点击。因此,一旦激活了下拉菜单,我就会选择我想要的选项,现在我只需要在该位置单击鼠标,但我不知道该怎么做。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains as AC
from selenium.webdriver.common.keys import Keys

nextButton = browser.find_element_by_xpath('//*[@id="rendererColorClassifyMethodDropdown"]')
nextButton.click()

nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
AC.click()

这会导致这个错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: click() missing 1 required positional argument: 'self'

向上滚动正在工作,因为我可以看到监视 python 命令,但是一旦我在那里就无法单击该选项。使用 nextButton.click() 只会重置下拉菜单。

这是点击后调用下拉菜单的 html:

<input class="dijitReset dijitInputInner" type="text" autocomplete="off" data-dojo-attach-point="textbox,focusNode" role="textbox" aria-autocomplete="both" aria-required="true" tabindex="0" id="rendererColorClassifyMethodDropdown" value="" aria-invalid="false" readonly="" style="cursor: pointer;">

紧随其后的标签是:

<input type="hidden" value="Natural Breaks">

当我使用箭头键滚动时,第二个标签的值会更改为当前滚动值的任何值,但无法在实际下拉显示中找到可点击值的 html 元素。

这是一张图片:

【问题讨论】:

  • 下拉列表的元素没有暴露在html中,因此找不到点击。什么意思?否则如何与它们互动?
  • 这是一个奇怪的 dijit 属性。我为下拉菜单添加了 html 标记以及它显示为当前值的内容。 html 中没有显示我可以看到的值的列表。
  • 没关系。我可以使用我发现的回车。

标签: python selenium-webdriver


【解决方案1】:

我正在回答我自己的问题,以防其他人发现自己处于这种情况并准备将他们的笔记本电脑扔出窗外。

不要为点击而烦恼。使用“输入”

nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ARROW_UP)
nextButton.send_keys(Keys.ENTER)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2015-03-26
    相关资源
    最近更新 更多