【发布时间】:2019-08-29 10:36:59
【问题描述】:
我正在尝试在 python 中按下带有 selenium 的按钮。我无法使用 xpath 或 css 选择器找到它。
对于脚本中的其他内容,我一直在使用 xpath,它工作正常,但每次打开此按钮的 xpath 似乎都是相对的。因此,我尝试使用 css 选择器访问。
我尝试访问的元素如下所示:
<button type="button" data-dismiss="modal" class="btn btn-primary pull-right">Opret AnnonceAgent</button>
chrome中inspect的css选择器:
#\35 d167939-adc2-0901-34ea-406e6c26e1ab > div.modal-footer > div > button.btn.btn-primary.pull-right
如果我应该发布更多 html,请告诉我。
我尝试了许多堆栈 oveflow 问题,并尝试了 css 选择器的许多变体,例如:
driver.find_element_by_class_name('#\35 d167939-adc2-0901-34ea-406e6c26e1ab.div.modal-footer.div.button.btn.btn-primary.pull-rightdiv.button.btn.btn-primary-pull-right').click()driver.find_element_by_class_name('div.button.btn.btn-primary-pull-right').click()driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()driver.find_element_by_class_name('btn.btn-primary-pull-right').click()driver.find_element_by_class_name('btn-primary-pull-right').click()
我也试过睡眠定时器。
该按钮位于当您按下前一个按钮时打开的窗口中,如果有帮助,背景会显示为灰色。 Picture.
# This opens up the window in which to press the next button (works fine)
button = driver.find_element_by_xpath('//*[@id="content"]/div[1]/section/div[2]/button')
button.click()
driver.implicitly_wait(15)
time.sleep(5)
# This is what doesn't work
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
我希望程序按下按钮,但它没有,它就在那里。
【问题讨论】:
-
试试看是否有效
driver.find_element_by_css_selector('button.btn.btn-primary.pull-right').click() -
谢谢,但还是不行。我得到
AttributeError: 'list' object has no attribute 'click',假设因为它是元素[s] - 这是故意的吗? -
打错字了。再试一次
driver.find_element_by_css_selector('button.btn.btn-primary.pull-right').click() -
不行,我得到
NoSuchElementException -
你能检查一下是否有框架阻止定位按钮吗?
标签: python selenium selenium-webdriver webdriver webdriverwait