पायथन के साथ सेलेनियम में एक वेबइमेन्ट के सभी बच्चों को एक सूची के रूप में प्राप्त करना संभव है?
जवाबों:
हाँ, आप इसे प्राप्त कर सकते हैं find_elements_by_css_selector("*")या find_elements_by_xpath(".//*")।
हालांकि, यह किसी तत्व के सभी बच्चों को खोजने के लिए एक वैध उपयोग के मामले की तरह नहीं है । सभी प्रत्यक्ष / अप्रत्यक्ष बच्चों को प्राप्त करना एक महंगा ऑपरेशन है। कृपया आगे बताएं कि आप क्या करने की कोशिश कर रहे हैं। एक बेहतर तरीका होना चाहिए।
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.stackoverflow.com")
header = driver.find_element_by_id("header")
# start from your target element, here for example, "header"
all_children_by_css = header.find_elements_by_css_selector("*")
all_children_by_xpath = header.find_elements_by_xpath(".//*")
print 'len(all_children_by_css): ' + str(len(all_children_by_css))
print 'len(all_children_by_xpath): ' + str(len(all_children_by_xpath))
हां, आप find_elements_by_बच्चों के तत्वों को एक सूची में पुनः प्राप्त करने के लिए उपयोग कर सकते हैं । यहाँ अजगर बाँध देखें: http://selenium-python.readthedocs.io/locating-elements.html
उदाहरण HTML:
<ul class="bar">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
आप इस find_elements_by_तरह का उपयोग कर सकते हैं :
parentElement = driver.find_element_by_class_name("bar")
elementList = parentElement.find_elements_by_tag_name("li")
यदि आप किसी विशिष्ट मामले में सहायता चाहते हैं, तो आप अपने पोस्ट को उस HTML से संपादित कर सकते हैं जिसे आप माता-पिता और बच्चों के तत्वों से प्राप्त करना चाहते हैं।
यहाँ बाल तत्वों को प्राप्त करने के लिए एक कोड है (जावा में):
String childTag = childElement.getTagName();
if(childTag.equals("html"))
{
return "/html[1]"+current;
}
WebElement parentElement = childElement.findElement(By.xpath(".."));
List<WebElement> childrenElements = parentElement.findElements(By.xpath("*"));
int count = 0;
for(int i=0;i<childrenElements.size(); i++)
{
WebElement childrenElement = childrenElements.get(i);
String childrenElementTag = childrenElement.getTagName();
if(childTag.equals(childrenElementTag))
{
count++;
}
}
.पहले//महत्वपूर्ण है, अगर डॉट गायब है, भले ही आप किसी तत्व से विधि को कॉल कर रहे हों, यह फिर से पूरे HTML को