owners = (By.CSS_SELECTOR,'.dropdown-toggle') self.highlight(owners) self.driver.find_element(owners).click()
时间: 2024-04-03 22:35:10 浏览: 80
在这段代码中,你传递给 `find_element` 函数的参数类型不正确。`find_element` 函数需要接收两个参数,第一个参数是一个定位器(locator),用于指定查找元素的方式,它可以是一个 By 对象或者是一个元组,元组中的第一个元素是查找元素的方式(如 By.ID、By.CSS_SELECTOR 等),第二个元素是定位元素的表达式(如 CSS 选择器、xpath 等)。第二个参数是一个等待时间,表示如果在指定的时间内没有找到元素,就会抛出超时异常。
根据你的代码,你已经正确地定义了一个元组 `owners = (By.CSS_SELECTOR, '.dropdown-toggle')`,但是在调用 `find_element` 函数的时候,你应该把 `owners` 作为一个参数传递,而不是把整个元组传递,因此你需要在调用 `find_element` 函数的时候,使用 `*owners` 的方式将元组展开为两个独立的参数。修改后的代码如下所示:
```python
owners = (By.CSS_SELECTOR, '.dropdown-toggle')
self.highlight(owners)
self.driver.find_element(*owners).click()
```
这样就可以正确地使用 `find_element` 函数来查找元素了。
相关问题
def test_search_none(self,keyword): # 搜索 # owner_list = self.owner_page.search(keyword) msg = self.owner_page.search_none(keyword) assert 'No owners' in msg
这段代码缺少一个必要的参数,应该这样写:
```python
def test_search_none(self):
keyword = "test"
msg = self.owner_page.search_none(keyword)
assert 'No owners' in msg
```
在调用`test_search_none`函数时,应该传入一个`self`参数,但是这个函数还需要一个`keyword`参数。在这个修正后的代码中,我在函数内部直接定义了`keyword`值,因此在外部调用函数时不需要传入`keyword`参数。
self = <tests.petclinic.test_owner.test_onwer_search.TestOwnerSearch object at 0x0000020ED2C4C5B0> keyword = 'balck' @pytest.mark.parametrize('keyword',[ 'balck', 'avis' ]) def test_search_none(self,keyword): # 搜索 # owner_list = self.owner_page.search(keyword) msg = self.owner_page.search_none(keyword) > assert 'No owners' in msg E TypeError: argument of type 'NoneType' is not iterable test_onwer_search.py:44: TypeError
这个错误是因为`msg`为`None`,而在`assert`语句中尝试迭代`None`类型的值,导致了`TypeError`异常。这个问题可能是由于`self.owner_page.search_none(keyword)`返回了`None`,而不是预期的字符串类型。
可以在`self.owner_page.search_none(keyword)`方法中打印一些调试信息,以便查看返回的值是否为`None`。例如:
```python
def search_none(self, keyword):
self.search(keyword)
try:
WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.XPATH, "//tbody")))
except TimeoutException:
return "No owners"
else:
return None
```
然后,您可以在测试函数中打印`msg`的值,查看它是否为`None`。如果是,则需要在`search_none`方法中进行调整,以确保它返回预期的字符串类型。
阅读全文
相关推荐















