使用 Selenium 单击启用 ember.js 的元素 [英] Click on ember.js enabled element using Selenium
问题描述
我正在尝试使用 selenium
在链接页面上单击以下按钮:
I am trying to click on the following button on a linkedin page using 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')
,即使类名正确也会失败?
driver.find_element_by_id
, but the id of the button seems to keep changing numberdriver.find_element_by_xpath
, but this contains the button number, so also failsdriver.find_element_by_class_name('share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view')
, this fails even though the class name is correct ?
基本上,所有方法都会生成相同的错误消息:
Basically, all methods generate the same error message:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}
我也试过 xpath contains() 方法,但是没有找到按钮.
I have also tried the xpath contains() method, but this does not find the button.
请问点击此按钮的正确方法是什么?
What would be the correct way to click on this button please ?
我在 windows
上使用 python 版本 3.9
和 driver = webdriver.Chrome
I am using python version 3.9
on windows
with driver = webdriver.Chrome
推荐答案
元素是一个 Ember.js 启用元素.因此,要在文本为 Post 的元素上 click()
,您可以使用以下任一 定位器策略:
The element is an Ember.js enabled element. So to click()
on the element with text as Post you can use either of the following Locator Strategies:
使用
css_selector
:
driver.find_element_by_css_selector("button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text").click()
使用xpath
:
driver.find_element_by_xpath("//button[contains(@class, 'share-actions__primary-action') and @data-control-name='share.post']/span[@class='artdeco-button__text' and contains(., 'Post')]").click()
理想情况下,要单击需要诱导的元素 WebDriverWait 用于 element_to_be_clickable()
,您可以使用以下任一定位器策略:
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.share-actions__primary-action[data-control-name='share.post']>span.artdeco-button__text"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'share-actions__primary-action') and @data-control-name='share.post']/span[contains(., 'Post')]"))).click()
注意:您必须添加以下导入:
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关的详细讨论:
You can find a couple of relevant detailed discussions in:
- Selenium - 基于 ember 查找元素莉>
- 自动化 Ember.在运行时更改对象属性时使用 Selenium 的 Node.js 应用程序
- Ember:最佳使用 Selenium 在浏览器中进行集成测试的实践
- Ember dropdown selenium xpath
这篇关于使用 Selenium 单击启用 ember.js 的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!