英文:
How to get all direct child elements of current element via CSS Selectors?
问题
I can help you with the translation of the code and the question part. Here is the translated text:
# 我想使用Selenium获取某个特定元素的所有子元素。假设我有以下HTML代码:
<html>
<table>
<tr>
<td>Test 1</td>
<td id="123">Test <b>2</b>
<span>span_text</span>
</td>
<td>Test 3</td>
</tr>
</table>
</html>
首先,我想通过其id找到第二个td元素,然后我想找到其所有子元素(即b和span元素)。要找到第二个td元素并不困难:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
page_source = """<table>
<tr>
<td>Test 1</td>
<td id="123">Test<b>2</b><span>span_text</span></td>
<td>Test 3</td>
</tr>
</table>
"""
driver = webdriver.Chrome()
driver.execute_script('document.documentElement.innerHTML=arguments[0]', f"<html>{page_source}</html>")
element = driver.find_element(By.ID, "123")
为了找到子元素,我尝试了td > *
CSS选择器:
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(element.tag_name)
但是,这会打印两个td
标签,而不是b
和span
。我还尝试了. > *
CSS选择器,但似乎无效。如何在Selenium中使用CSS选择器选择一个WebElement的所有(直接)子元素?
注意:我想使用element.find_elements()
方法,即我希望Selenium仅在已找到的元素内进行搜索。
I've translated the code and the question as requested.
<details>
<summary>英文:</summary>
I'd like to get all child elements of some specific element with Selenium. Let's say I have the following HTML:
<html>
<table>
<tr>
<td>Test 1</td>
<td id="123">Test <b>2</b>
<span>span_text</span>
</td>
<td>Test 3</td>
</tr>
</table>
</html>
First, I'd like to find the second td element by its id and then I'd like to find all of its child elements (i.e. the b and span elements). Finding the second td element isn't hard:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
page_source = """<table>
<tr>
<td>Test 1</td>
<td id="123">Test<b>2</b><span>span_text</span></td>
<td>Test 3</td>
</tr>
</table>
"""
driver = webdriver.Chrome()
driver.execute_script('document.documentElement.innerHTML=arguments[0]', f"<html>{page_source}</html>")
element = driver.find_element(By.ID, "123")
In order to find the child elements, I tried the td > *
CSS Selector:
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(element.tag_name)
However, this prints two td
tags instead of b
and span
. I also tried the . > *
CSS Selector instead, which seems to be invalid. How I can select all (direct) child elements of an WebElement in Selenium by CSS Selectors?
Note: I want to use the element.find_elements()
method, i.e. I want selenium to only search within the already found element.
答案1
得分: 1
以下是翻译好的部分:
你差不多已经到位了。你的代码完美无缺。你只需要在每次迭代中保持在 `for` 语句中引用后代为 _`elements`_。因此,你需要调用:
print(elements.tag_name) # 而不是 element,应该是 elements
下面是完整的程序:
page_source = """<table>
<tr>
<td>Test 1</td>
<td id="123">Test<b>2</b><span>span_text</span></td>
<td>Test 3</td>
</tr>
</table>
"""
driver = webdriver.Chrome(service=s, options=options)
driver.execute_script('document.documentElement.innerHTML=arguments[0]', f"<html>{page_source}</html>")
element = driver.find_element(By.ID, "123")
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(elements.tag_name)
输出:
b
span
希望这对你有所帮助。如果有任何其他问题,请随时提出。
英文:
You were almost there. Your code is perfect. You just need to keep a track that within for
statement in each iteration the decendants are referred as elements
. So you have to invoke:
print(elements.tag_name) # instead of element, it should be elements
Here is the complete program:
page_source = """<table>
<tr>
<td>Test 1</td>
<td id="123">Test<b>2</b><span>span_text</span></td>
<td>Test 3</td>
</tr>
</table>
"""
driver = webdriver.Chrome(service=s, options=options)
driver.execute_script('document.documentElement.innerHTML=arguments[0]', f"<html>{page_source}</html>")
element = driver.find_element(By.ID, "123")
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(elements.tag_name)
Output:
b
span
答案2
得分: 1
There's a bug in your code
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(element.tag_name)
^ elements
This should be elements.tag_name
(plural of element) to use the loop variable, elements
. Your variable on the previous line is element
so you were printing the TD from the previous line twice.
Having said that, you can do this in a single step and simplify your code and save a little time
for element in driver.find_elements(By.CSS_SELECTOR, "[id='123'] > *"):
print(element.tag_name)
英文:
There's a bug in your code
for elements in element.find_elements(By.CSS_SELECTOR, f"td > *"):
print(element.tag_name)
^ elements
This should be elements.tag_name
(plural of element) to use the loop variable, elements
. Your variable on the previous line is element
so you were printing the TD from the previous line twice.
Having said that, you can do this in a single step and simplify your code and save a little time
for element in driver.find_elements(By.CSS_SELECTOR, "[id='123'] > *"):
print(element.tag_name)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论