Python Selenium: 使用自定义属性点击元素

huangapple go评论63阅读模式
英文:

Python Selenium: Click element by custom attributes

问题

我正在尝试使用Selenium点击这个按钮:

<button class="MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary" tabindex="0" type="button" data-test="unifiedCrazyButton"><span class="MuiButton-label">让我们疯狂一下</span><span class="MuiTouchRipple-root"></span></button>

我无法通过类名来做到这一点,因为有其他具有相同类名的按钮,而且这个格式的xpath值:

/html/body/div[10]/div[3]/div/div[3]/button[2]

会不断变化,这很不幸。

唯一可识别的因素似乎是:

data-test="unifiedCrazyButton"

如何使用Selenium点击这个按钮?

英文:

I am trying to click this button with Selenium:

&lt;button class=&quot;MuiButtonBase-root MuiButton-root jss38 MuiButton-contained MuiButton-containedPrimary&quot; tabindex=&quot;0&quot; type=&quot;button&quot; data-test=&quot;unifiedCrazyButton&quot;&gt;&lt;span class=&quot;MuiButton-label&quot;&gt;Let&#39;s get crazy&lt;/span&gt;&lt;span class=&quot;MuiTouchRipple-root&quot;&gt;&lt;/span&gt;&lt;/button&gt;

I can't do it by the class name, because there are other buttons with the same class name, and also the xpath value in this format:

/html/body/div[10]/div[3]/div/div[3]/button[2]

keeps changing which is unfortunate.

The only identifying factor seems to be

data-test=&quot;unifiedCrazyButton&quot;

How can I click this button with Selenium?

答案1

得分: 0

请注意:您需要添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

要点击可点击元素,由于所需元素是一个动态元素,因此您需要使用WebDriverWait来等待元素变为可点击状态(使用element_to_be_clickable()方法),并且您可以使用以下任一定位策略

  • 使用_CSS_SELECTOR_:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-test='unifiedCrazyButton'] span.MuiButton-label"))).click()
  • 使用_XPATH_:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-test='unifiedCrazyButton']//span[@class='MuiButton-label' and contains(., 'get crazy')]"))).click()

这将点击所需的元素。

英文:

The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;button[data-test=&#39;unifiedCrazyButton&#39;] span.MuiButton-label&quot;))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, &quot;//button[@data-test=&#39;unifiedCrazyButton&#39;]//span[@class=&#39;MuiButton-label&#39; and contains(., &#39;get crazy&#39;)]&quot;))).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
    

huangapple
  • 本文由 发表于 2023年2月10日 07:47:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定