如果颜色匹配,如何单击按钮

2024-06-09 19:39:24 发布

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

我需要点击一个按钮,如果它的颜色是“139,140,199”,我已经尝试过Pyatogui,但从来没有工作过,我可以用selnium吗

    def mute_cam(self):
        time.sleep(3)
        cam_xpath = '/html/body/div[2]/div[2]/div[2]/div[1]/div/calling-pre-join-screen/div/div/div[2]/div[1]/div[2]/div/div/section/div[2]/toggle-button[1]/div/button/span[1]'
        self.rgb = driver.find_element_by_xpath(cam_xpath).value_of_css_property('background')
        list = [self.rgb]
        str_list = str(self.rgb)
        new_list = str_list[4:17]
        print(new_list)

我曾尝试将颜色存储在一个列表中,然后进行比较,但问题是我不知道怎么做


Tags: selfdivnew颜色defbuttonrgb按钮
1条回答
网友
1楼 · 发布于 2024-06-09 19:39:24

您可以通过Javascript或使用.value_of_css_property在Selenium中获得元素的任何CSS属性

下面是获取引导文档page上的按钮列表并获取其background-color的示例

driver.get("https://getbootstrap.com/docs/4.0/components/buttons/")

btn_list = driver.find_elements_by_xpath("//div[@class = 'bd-example'][1]/button")

for itm in range(0, len(btn_list)):
    name = btn_list[itm].text
    print(name)
    color = btn_list[itm].value_of_css_property("backgroundColor")
    print(color)

    if color == "rgba(0, 123, 255, 1)":
    # do any action    
    print("Button with Primary color detected")

上述代码的输出为:

Primary
rgba(0, 123, 255, 1)
Button with Primary color detected
Secondary
rgba(108, 117, 125, 1)
Success
rgba(40, 167, 69, 1)
Danger
rgba(220, 53, 69, 1)
Warning
rgba(255, 193, 7, 1)
Info
rgba(23, 162, 184, 1)
Light
rgba(248, 249, 250, 1)
Dark
rgba(52, 58, 64, 1)
Link
rgba(0, 0, 0, 0)

Process finished with exit code 0

相关问题 更多 >