如何修复选择输入字段时出现的“不允许复合类名”异常

2024-06-26 04:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的硒,想用它来测试登录页。我有下面的文本字段,我想在其中插入用户名。你知道吗

<div class="WODM WNDM" data-automation-id="userName">
<div class="gwt-Label WBEM">Email Address</div><input type="text" 
class="gwt-TextBox WAEM" autocapitalize="none" autocorrect="off" 
autocomplete="off" aria-label="Email Address" dir="ltr">
</div>

我写了以下代码,但它不起作用:

emailElem = driver.find_element_by_class_name('gwt-Label WBEM')
emailElem.send_keys('some_email')

提前谢谢!你知道吗


Tags: 文本divdataaddressemaillabel用户名class
2条回答

我能看到三件事:

  1. 应该在发送密钥之前调用clear()。你知道吗
  2. 您已经输入了两个类,就像您在HTML中看到的那样-我非常确定selenium只希望调用中有一个类,因此选择了明智的方法(请确认这一点-如果不是太麻烦,请确定。你知道吗
  3. 我看到你试图选择的类是gwt-LabelWBEM,这是一个<label></label>-你不应该调用<input></input>,它有两个类:gwt-TextBoxWAEM

因此,考虑到这三点,您应该将代码更改为:

emailElem = driver.find_element_by_class_name('gwt-TextBox')
emailElem.clear()
emailElem.send_keys('some_email')

可以使用xpath,如下所示

driver.find_element_by_xpath("//input[@type='text' and @class='gwt-TextBox WAEM']")

相关问题 更多 >