首页 > 解决方案 > 单击 xpath 中具有动态 url 更改的元素

问题描述

我写的下面的css选择器。

.custom-scrollbar .content-row:nth-of-type(1) [src='https://static-qa.xxxxxxxxx.com/csp/images/rotate_action_icon.svg']

在此处输入图像描述

问题是我需要点击那个编辑按钮,但问题是 url 相对于 env 的变化。示例-当前它指向 qa,因此 url 是 qa,如果它在 prod 中,那么它将由于发生未找到 web 元素错误而改变。

我正在尝试使用以下不起作用的代码-

List<WebElement> list = pilot.findElements(By.tagName("img"));
        String aa = list.get(3).getAttribute("src");
        list.get(2).click();

请建议一种更好的方法来使用这些带有动态 url 的按钮。

标签: javaseleniumselenium-webdriver

解决方案


尝试使用以下 css 选择器startswithendswith

img[src^='https://static-'][src$='com/csp/images/rotate_action_icon.svg']

推荐阅读