单击使用Selenium启用ember.js的元素 [英] Click on ember.js enabled element using Selenium
问题描述
我正在尝试使用硒
在linkedin页面上单击以下按钮:
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--primer 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
上使用 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()
理想情况下,要单击该元素,您需要诱导定位器策略:
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.js的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!