python - 单击使用 Selenium 启用 ember.js 的元素
问题描述
我正在尝试使用以下按钮在linkedin页面上单击selenium
:
<button id="ember607" class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view" data-control-name="share.post"><!---->
<span class="artdeco-button__text">
Post
</span></button>
我曾尝试使用:
driver.find_element_by_id
,但按钮的 id 似乎不断变化的数字driver.find_element_by_xpath
,但这包含按钮编号,所以也失败了driver.find_element_by_class_name('share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view')
,即使类名正确,这也会失败?
基本上,所有方法都会生成相同的错误消息:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}
我也尝试过 xpath contains() 方法,但这没有找到按钮。
请问点击这个按钮的正确方法是什么?
我正在使用3.9
pythonwindows
版本driver = webdriver.Chrome
解决方案
有时,目前无法点击的按钮会出现问题。试试这个:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.XPATH, '[YOUR X_PATH TO THE BUTTON]')))
driver.execute_script("arguments[0].click()", button)
这不是用硒单击任何按钮的最干净的方法,但对我来说,这种方法几乎每次都有效。
推荐阅读
- python - “OLS”对象没有“摘要”属性
- c# - VS 中的单元测试/无法看到失败测试的完整错误消息
- javascript - 在 div 中添加多个元素,而其他元素不会丢失其值
- docker - Google Cloud 和 Oracle Cloud VPC 上 Docker 群管理器的问题
- scala - 如何在spark中拆分数据不一致的列
- angular - 键盘导航时DxDataGrid不会触发routerLink
- android - AndroidX Android Studio 3.5.2 设置actionbar title Null
- eclipse - Eclipse 立即取消删除已删除的文件
- php - 如果任何结果与mysql结果匹配,如何从表中获取数据
- r - 提取行名的一部分以在 R 的数据框中创建一个新列