有没有一种方法可以使用Selenium python将具有值的新属性添加到元素? [英] Is there a way to add a new attribute with a value to an element using selenium python?

查看:159
本文介绍了有没有一种方法可以使用Selenium python将具有值的新属性添加到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是HTML

<img src="//www.shahidpro.tv/uploads/articles/220cc817.jpg" width="408" height="605" vspace="" hspace="" border="0" alt="">

我正在尝试添加3个新属性以使其像

I am trying to add 3 new attributes to make it like

<img src="//www.shahidpro.tv/uploads/articles/220cc817.jpg" width="408" height="605" vspace="" hspace="" border="0" alt="" style="display: block; margin-left: auto; margin-right: auto;" data-mce-style="display: block; margin-left: auto; margin-right: auto;" data-mce-selected="1">

我要添加:

  • style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-selected="1"
  • style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-style="display: block; margin-left: auto; margin-right: auto;" and
  • data-mce-selected="1"

我尝试过:

image = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body.mce-content-body#tinymce > p/a/img")))
browser.execute_script("arguments[0].style='display: block; margin-left: auto; margin-right: auto;'", image)
browser.execute_script("arguments[0].data-mce-style='display: block; margin-left: auto; margin-right: auto;'", image)
browser.execute_script("arguments[0].data-mce-selected='1'", image)     ` but got no results nor errors

推荐答案

要添加三个新属性,您需要使用

To add the three new attributes you need to use Selenium's execute_script() method.

现在,您要添加以下属性:

Now, you want to add the following attributes:

  • style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-selected="1"
  • style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-style="display: block; margin-left: auto; margin-right: auto;"
  • data-mce-selected="1"

添加它们的方法将是相似的,并且作为添加属性data-mce-selected="1"的演示,您需要引入定位器策略:

The method for adding them would be similar and as a demonstration to add the attribute data-mce-selected="1" you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • 使用CSS_SELECTOR:

element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img[src='//www.shahidpro.tv/uploads/articles/220cc817.jpg']")))
browser.execute_script("arguments[0].setAttribute('value','28/02')", element)

  • 使用XPATH并在一行中:

  • Using XPATH and in a single line:

    browser.execute_script("arguments[0].setAttribute('data-mce-selected','1')", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[@src='//www.shahidpro.tv/uploads/articles/220cc817.jpg']"))))
    

  • 您可以在以下位置找到几个相关的详细讨论:

    You can find a couple of relevant detailed discussions in:

    • Selenium Datepicker using JavascriptExecutor
    • How to use javascript to set attribute of selected web element using selenium Webdriver using java?
    • Python - How to edit href attribute in source code before clicking using Selenium

    这篇关于有没有一种方法可以使用Selenium python将具有值的新属性添加到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆