Python 使用Selenium单击ember.js启用的元素

Python 使用Selenium单击ember.js启用的元素,python,selenium,xpath,ember.js,css-selectors,Python,Selenium,Xpath,Ember.js,Css Selectors,我正在尝试使用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"><!----> <sp

我正在尝试使用
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>
我还尝试了xpath contains()方法,但没有找到按钮

点击这个按钮的正确方法是什么

我在
windows
上使用python版本
3.9
driver=webdriver.Chrome

//button[@class="share-actions__primary-action artdeco-button artdeco-button--2 artdeco-button--primary ember-view"]. 


有时按钮出现问题,此时无法单击。 试试这个:

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单击任何按钮都不是最干净的方法,但对我来说,这种方法几乎每次都有效。

有时按钮出现问题,此时无法单击。 试试这个:

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单击任何按钮都不是最干净的方法,但对我来说,这种方法几乎每次都有效。

通过xpath,这应该可以:

//button/span[contains(text(), "Post")]
将其与等待元素相结合:

button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
    )

by类选择器的问题是多个类名。请参阅此问题:有关如何克服此问题的更多详细信息。

通过xpath,这应该可以:

//button/span[contains(text(), "Post")]
将其与等待元素相结合:

button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button/span[contains(text(), "Post")]"))
    )

by类选择器的问题是多个类名。请参阅此问题:有关如何克服此问题的更多详细信息。

通过Post找到跨度,然后单击其按钮标记

//span[contains(text(), 'Post')]/parent::button

找到带有Post的跨度,然后单击其按钮标记

//span[contains(text(), 'Post')]/parent::button
该元素是已启用的元素。因此,要
在文本为Post的元素上单击()

  • 使用
    css\u选择器

    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()
    

理想情况下,要单击需要引导的元素,使
元素可单击()
,您可以使用以下任一选项:

  • 使用
    CSS\u选择器

    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
    

工具书类 您可以在以下内容中找到一些相关的详细讨论:

该元素是已启用的元素。因此,要
在文本为Post的元素上单击()

  • 使用
    css\u选择器

    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()
    

理想情况下,要单击需要引导的元素,使
元素可单击()
,您可以使用以下任一选项:

  • 使用
    CSS\u选择器

    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
    

工具书类 您可以在以下内容中找到一些相关的详细讨论:



您的意思是这样将其放入xpath中:
驱动程序。通过xpath('//button[@class=“share-actions\uuu\u primary-action artdeco button artdeco button--2 artdeco button--primary ember view”])查找元素。
。如果是这样,则返回相同的错误消息。不能使用
//按钮[contains(@id,'ember')]
建议,因为有许多按钮带有
ember
+
一些随机数
。尝试对第一个xpath使用显式等待,然后将其放入xpath中,就像这样:
驱动程序。通过xpath('//button[@class])查找元素=“share-actions\uuu primary-action artdeco button artdeco button--2 artdeco button--primary ember view”]
。如果是这样,则返回相同的错误消息。无法使用
//按钮[包含(@id,'ember')]
建议,因为有许多按钮带有
ember
+
一些随机数
。尝试对第一个xpath使用显式等待,然后
消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//button/span[contains(text(),'Post')]}
…如果是//span,它能工作吗?如果添加等待(我编辑了答案)它能工作吗?不,它会给出不同的错误:
发生异常:TypeError\uuuu init\uuu()接受2个位置参数,但给出了3个。
。注意:我将一组单引号更正为双引号。我有一个打字错误。为您更新。
消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“//button/span[contains(text(),“Post”)]”}
…如果是//span,是否有效?如果添加等待,是否有效(我编辑了答案)不,它给出了一个不同的错误:
发生了异常:TypeError\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()接受了2个位置参数,但给出了3个位置参数。
。注意:我将一组单引号改为双引号。我有一个拼写错误。为您更新了。我这样做
驱动程序。通过xpath(//span[contains(text(),'Post')]/parent::button)查找\u元素).click()
但获取此错误
发生异常:NoSuchElementException消息:没有此类元素:无法定位元素:{“方法”:“xpath”,“选择器”:“//span[contains(text(),'Post')]]/parent::button”}
我执行此操作
驱动程序。通过xpath(//span[contains(text(),'Post')]/parent::button)查找元素。单击()
但获取此错误
发生异常:NoSuchElementException消息:无此类元素:无法定位元素:{“方法”:“xpath”,“选择器”:“//span[contains(text(),'Post')]/parent::button”}
此操作:
WebDriverWait(driver,20)。直到(EC.element_to_可点击((By.xpath),//button[contains(@class,'share-actions\uu primary-action')和@data-control name='share.post']/span[contains(,'post')])。单击()
,导入如上所述的内容。谢谢欢迎您,如果您认为这个问题对其他人有帮助,请向上投票。这项工作:
WebDriverWait(driver,20)。直到(EC.element\u可单击((By.XPATH),//按钮[contains](@class,'share-actions\uu primary-action')和@data-control name='share.post']/span[contains(,'post')])。单击()
,导入如上所述。谢谢欢迎您,请向上投票