You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium多条件定位元素:点击指定行内符合条件的div

How to Locate and Click the Target Div in Selenium

To solve this problem, we need to target a <div> with ng-click="condition001" that resides in the same table row (<tr>) as a link containing the text "Auto-001". Here's a straightforward approach using XPath to tie these two conditions together:

Craft the Correct XPath Expression

The key is to first identify the row that contains the link with "Auto-001", then find the target div within that row. Use this XPath:

//tr[.//a[normalize-space(text())='Auto-001']]//div[contains(@ng-click,'condition001')]

Let's break this down:

  • //tr[.//a[normalize-space(text())='Auto-001']]: Locates the <tr> element that has a child <a> tag with the normalized text "Auto-001" (normalizing handles any extra whitespace in the link text).
  • //div[contains(@ng-click,'condition001')]: Finds the <div> inside that specific row whose ng-click attribute includes "condition001".

Implement in Selenium

Here's how you'd use this XPath in Python Selenium to click the element:

from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize your driver (example using Chrome)
driver = webdriver.Chrome()

# Locate and click the target element
target_div = driver.find_element(By.XPATH, "//tr[.//a[normalize-space(text())='Auto-001']]//div[contains(@ng-click,'condition001')]")
target_div.click()

If you're using Java, the code would look like this:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TargetElementClick {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.findElement(By.xpath("//tr[.//a[normalize-space(text())='Auto-001']]//div[contains(@ng-click,'condition001')]")).click();
    }
}

Additional Notes

  • If the link text is exactly "Auto-001" with no extra spaces, you can omit normalize-space() and just use text()='Auto-001'.
  • If the DOM structure varies slightly (e.g., the link is nested deeper), adjust the .//a part to match the actual hierarchy relative to the <tr>.

内容的提问来源于stack exchange,提问作者Ishita Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:53:53