选择按钮或文本操作的Python

2024-04-27 04:45:28 发布

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

我是,可怜的,试图1。单击一个或两个按钮。检查文本,然后执行操作。我想我只是编码不对。你知道吗

例如

if driver.find_element_by_class_name('classOne').click()
elif:
    "No Item" in driver.find_element_by_class_name('classTwo').driver.get(self_base_url)

这看起来太简单了,我肯定我做错了什么。这个“应该”有用,但不是吗?你知道吗


Tags: noname文本编码byifdriverelement
1条回答
网友
1楼 · 发布于 2024-04-27 04:45:28

如果driver找不到元素,它将引发^{}。你知道吗

在您的情况下,您可以:

x = driver.find_elements_by_class_name('classOne')
if len(x) > 0:
    # click the first one found
    x[0].click()
else:
    print('No item x was found.')

注意,我把它从find_element_by_class_name改成了find_elements_by_class_name。这将查找所有元素(如果有),并返回一个列表。你知道吗

相关问题 更多 >