简体   繁体   English

Selenium WebDriver 使用 CSS 选择器和数据属性找不到元素

[英]Selenium WebDriver not finding element using CSS Selector and data attribute

I have an input HTML element for first name in signup.我有一个输入 HTML 元素作为注册中的名字。

 <div class="form-group" data-test="signup-form-first-name"> <label for="first-name">First Name</label> <input type="text" id="first-name" name="first-name" autocomplete="given-name" class="fbkr-input gray error" value="" background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: pointer;"> <div aria-live="polite" class="label label-danger"> First Name is required. </div> </div>

The element with label-danger class is only visible if there is an error and the field is empty.带有标签危险 class 的元素仅在出现错误且字段为空时可见。 I am trying to test whether that element is visible when testing the signup flow, but every time I try that, it fails.我正在尝试在测试注册流程时测试该元素是否可见,但每次我尝试时,它都会失败。 I tried to change the script so it only uses the class (.label-danger), but alas, still nothing.我尝试更改脚本,使其仅使用 class (.label-danger),但唉,仍然没有。 Here is the code that I am trying that with.这是我正在尝试的代码。

    def is_element_error_label_visible(self, element_data_attribute: str) -> bool:
        try:
            element_present = 
            expected_conditions.presence_of_element_located(
                (By.CSS_SELECTOR, f". 
            {self._build_data_attribute(element_data_attribute)} .label-danger"))
            WebDriverWait(self.remote_web_driver, 
            15).until(element_present)
        except NoSuchElementException:
            return False
        return True

The value that的价值

f"{self._build_data_attribute(element_data_attribute)} .label-danger"))

returns is: [data-test="signup-form-first-name"].label-danger返回是: [data-test="signup-form-first-name"].label-danger

Is this an invalid way to get the element using the CSS Selector?这是使用 CSS 选择器获取元素的无效方法吗? Or is there a much simpler way for this.或者有没有更简单的方法。 Please keep in my mind that I do not want to use xPath, since I like to keep my code clean, that is why I am using the data-test attribute.请记住,我不想使用 xPath,因为我喜欢保持代码干净,这就是我使用data-test属性的原因。

Try this following css selector.试试下面的 css 选择器。

expected_conditions.presence_of_element_located((By.CSS_SELECTOR, ".label-danger"))

Update: While Automating seems you need to click on Sign_up button to see the error without entering data.更新:虽然自动化似乎您需要单击 Sign_up 按钮才能在不输入数据的情况下查看错误。

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

driver = webdriver.Chrome()
driver.get("https://fishingbooker.com/")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.btn[href*='signup']"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.fbkr-button.yellow"))).click()
try:
    print(WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"#first-name + .label-danger"))).text)
except:
    print("No such element found")

Hope this helps.希望这可以帮助。

I'm not familiar with python to provide a code snippet.我不熟悉 python 来提供代码片段。 But I would use a partial CSS selector matcher for this situation.但是对于这种情况,我会使用部分 CSS 选择器匹配器。 Please use it if applicable only.请仅在适用时使用它。

div[class*="label-danger"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 CSS 选择器/硒查找元素 - Finding element using CSS Selector/Selenium Selenium AttributeError: 'WebDriver' object 没有属性 'find_element_by_css_selector' - Selenium AttributeError: 'WebDriver' object has no attribute 'find_element_by_css_selector' 使用 Selenium Webdriver 在元素中查找元素 - Finding an element within an element using Selenium Webdriver 如何使用Python中的Selenium WebDriver通过CSS选择器对元素的子项进行配对? - How to pair an element's children by CSS selector using Selenium WebDriver in Python? Selenium find_element_by_css_selector 找不到元素 - Selenium find_element_by_css_selector not finding element 如何修复“WebDriver”object 没有属性“通过 CSS 选择器查找元素”? - How to fix 'WebDriver' object has no attribute 'find element by CSS selector'? Selenium webdriver 帮助查找元素 - Selenium webdriver help finding a element Selenium Webdriver找不到收件箱元素 - Selenium webdriver is not finding the Inbox element 使用 Selenium 通过自定义按钮属性查找元素 - Finding element by custom button attribute using Selenium AttributeError: 'dict' object has no attribute 'click' error with find_element_by_css_selector() 和 click() 使用 Selenium 和 ChromeDriver - AttributeError: 'dict' object has no attribute 'click' error with find_element_by_css_selector() and click() using Selenium and ChromeDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM