单击 ember.js enabled element using Selenium
Click on ember.js enabled element using Selenium
我正在尝试使用 selenium
在 linkedin 页面上单击以下按钮:
<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')
,即使 class 名称正确,也会失败?
基本上,所有方法都会生成相同的错误消息:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}
我也试过 xpath contains() 方法,但是找不到按钮。
请问单击此按钮的正确方法是什么?
我在 windows
和 driver = webdriver.Chrome
上使用 python 版本 3.9
//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"].
或者
//button[contains(@id,'ember')]
有时会出现按钮暂时无法点击的问题。
试试这个:
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)
这不是用 selenium 单击任何 Button 的最干净的方法,但对我来说,这种方法几乎每次都有效。
通过 xpath 这应该可以工作:
//button/span[contains(text(), "Post")]
将它与等待元素结合起来:
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
)
您的 by class 选择器的问题是多个 class 名称。请参阅此问题:How to get elements with multiple classes 了解有关如何克服该问题的更多详细信息。
找到带有 Post 的 span 并点击它的按钮标签。
//span[contains(text(), 'Post')]/parent::button
该元素是一个 Ember.js enabled element. So to click()
on the element with text as Post you can use either of the following :
使用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 for the element_to_be_clickable()
and you can use either of the following :
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到一些相关的详细讨论:
- Selenium - Finding element based on ember
- Automate Ember.js application using Selenium when object properties are changed at run-time
- Ember: Best practices with Selenium to make integration tests in browser
我正在尝试使用 selenium
在 linkedin 页面上单击以下按钮:
<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')
,即使 class 名称正确,也会失败?
基本上,所有方法都会生成相同的错误消息:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element:{[*the_error_is_here*]}
我也试过 xpath contains() 方法,但是找不到按钮。
请问单击此按钮的正确方法是什么?
我在 windows
和 driver = webdriver.Chrome
3.9
//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"].
或者
//button[contains(@id,'ember')]
有时会出现按钮暂时无法点击的问题。 试试这个:
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)
这不是用 selenium 单击任何 Button 的最干净的方法,但对我来说,这种方法几乎每次都有效。
通过 xpath 这应该可以工作:
//button/span[contains(text(), "Post")]
将它与等待元素结合起来:
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
)
您的 by class 选择器的问题是多个 class 名称。请参阅此问题:How to get elements with multiple classes 了解有关如何克服该问题的更多详细信息。
找到带有 Post 的 span 并点击它的按钮标签。
//span[contains(text(), 'Post')]/parent::button
该元素是一个 Ember.js enabled element. So to click()
on the element with text as Post you can use either of the following
使用
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 for the element_to_be_clickable()
and you can use either of the following
使用
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到一些相关的详细讨论:
- Selenium - Finding element based on ember
- Automate Ember.js application using Selenium when object properties are changed at run-time
- Ember: Best practices with Selenium to make integration tests in browser