У меня есть следующий тип HTML-кода, с которым я могу работать:
<tbody id = "id_tbody">
<tr id = "tr_project_0" class = "project_tr clr01 hover" data-select-value = "0" style = "border-top: 1px dotted #B4B4B4;">
<td class = "txtC">
<a href = "/173537">173537</a>
</td>
<tr id = "tr_research_0" style = "" class = "">
<table id = "table_project_num_173537" class = "tblTypeInner01 cl_tr_research_node">
<tbody>
<tr id = "tr_research_node_173537_0" class = "research_tr" data-select-value = "442879,0,173537,2">
<td class = "txtC"><a href = "/442879">442879</a></td>И следующий код Python:
project_list = browser.find_element_by_xpath( "//tr[@class='project_tr clr01']" )
survey_list = project_list.find_elements_by_xpath( "//tr[@class='research_tr']" )
last_survey = survey_list[0]
survey_go = last_survey.find_elements_by_xpath( "//td[@class='txtC']" )
print survey_go[0].textЯ ожидаю, что сценарий вернет "442879", но он вернет "173537".
Почему survey_go возвращает субэлементы project_list вместо survey_list[0]?






Вам нужно указать точку в начале XPath, чтобы указать на контекстный узел (project_list):
survey_list = project_list.find_elements_by_xpath( ".//tr[@class='research_tr']" )
То же и для survey_go:
last_survey = survey_list[0]
survey_go = last_survey.find_elements_by_xpath( ".//td[@class='txtC']" )