[英]error driver.find_element or find_elements
I'm trying to click "Create New Network" by using selenium.我正在尝试使用 selenium 单击“创建新网络”。
<button type="button" id="dt-refreshBtn" class="btn wc-btn--link" data-label="Create New Network" role="link"><span class="icon-button" data-testid="dnxButton-iconButtonContainer" data-awt="networkListing-button-createNew"><i class="dnac-icon-add-circle" data-testid="dnxButton-icon" title="Create New Network"></i><span class="dnx-btn-icon-label" data-testid="dnxButton-iconLabel">Create New Network</span></span></button>
<span class="icon-button" data-testid="dnxButton-iconButtonContainer" data-awt="networkListing-button-createNew"><i class="dnac-icon-add-circle" data-testid="dnxButton-icon" title="Create New Network"></i><span class="dnx-btn-icon-label" data-testid="dnxButton-iconLabel">Create New Network</span></span>
<i class="dnac-icon-add-circle" data-testid="dnxButton-icon" title="Create New Network"></i>
<span class="dnx-btn-icon-label" data-testid="dnxButton-iconLabel">Create New Network</span>
I tried several scripts to find the location of "Create New Network" button, but got failed with below reason.我尝试了几个脚本来查找“创建新网络”按钮的位置,但因以下原因而失败。
here are scripts I've tried.这是我试过的脚本。
driver.find_element(By.CSS_SELECTOR, "[title='Create New Network']").click()
driver.find_element(By.CSS_SELECTOR, "[data-awt='networkListing-button-createNew']").click()
driver.find_element(By.CSS_SELECTOR, "[id='dt-refreshBtn']").click()
driver.find_element(By.CSS_SELECTOR, "[class='dnx-btn-icon-label']").click()
driver.find_elements(By.XPATH, "//*[@class='dnx-btn-icon-label']").send_keys(Keys.ENTER)
driver.find_elements(By.XPATH, "//button[@class='btn wc-btn--link']")[0].send_keys(Keys.ENTER)
driver.find_elements(By.XPATH, "//*[@id='dt-refreshBtn']").send_keys(Keys.ENTER)
driver.find_element(By.ID, "dt-refreshBtn").send_keys(Keys.ENTER)
driver.find_element(By.CSS_SELECTOR, "[data-testid='dnxButton-icon']").send_keys(Keys.ENTER)
driver.find_element(By.CSS_SELECTOR, "[data-testid='dnxButton-iconLabel']").send_keys(Keys.ENTER)
driver.find_elements(By.CSS_SELECTOR, "[data-awt='networkListing-button-createNew']").click()
driver.find_element(By.CSS_SELECTOR, "[title='Create New Network']").click()
driver.find_elements(By.XPATH, "//*[@id='dt-refreshBtn']").click()
could you please help this one?你能帮帮这个吗?
Now let's go through each error.find_elements() is used for multiple elements and.click() |现在让我们通过每个错误 go.find_elements() 用于多个元素和 .click() | send_keys() is used for a single element is why the majority will give 'list' object has no attribute 'click' unless you access the individual element.
send_keys() 用于单个元素是为什么大多数人会给出'list' object has no attribute 'click'除非您访问单个元素。
.send_keys() is normally used for input tags or textareas and you'd want.click() for the button tag. .send_keys() 通常用于输入标签或文本区域,您需要 .click() 作为按钮标签。
Now some valid xpaths would be like so:现在一些有效的 xpaths 会像这样:
driver.find_element(By.XPATH, "//button[@class='btn wc-btn--link']").click()
would be a valid xpath if that is the only button class with that class name.如果这是唯一具有 class 名称的按钮 class,则将是有效的 xpath。
driver.find_element(By.XPATH, "//button[@id='dt-refreshBtn']").click()
If this still doesn't find check if the element is under iframes or shadow roots.如果仍然找不到,请检查该元素是否在 iframe 或影子根下。
driver.find_element(By.XPATH, "//button[@id='dt-refreshBtn']").click()
should work应该管用
finally, I found the reason and got the solution in this code.最后,我在这段代码中找到了原因并得到了解决方案。
driver.find_element(By.XPATH, "//button[@id='dt-refreshBtn' and @class='btn wc-btn--link']")
with this combination, it worked.通过这种组合,它奏效了。 Thanks everyone.
谢谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.