考慮下面的HTML:硒蟒蛇選擇元素
<div class="nav-category__col" id="category_nav_level_3" style="display: block;">
<input type="hidden" value="1" name="videogame_type" id="videogame_type">
<ul class="nav-category__list" adparam_name="videogame_type" role="radiogroup">
<li class="nav-category__list-item nav-category__list-item--selected" id="1" tabindex="-1" role="radio" aria-checked="true">
<p class="nav-category__name">Consoles</p>
</li>
<li class="nav-category__list-item " id="2" tabindex="-1" role="radio" aria-checked="false">
<p class="nav-category__name">Jogos</p>
</li>
<li class="nav-category__list-item " id="3" tabindex="-1" role="radio" aria-checked="false">
<p class="nav-category__name">Acessórios</p>
</li>
</ul>
</div>
我想點擊<p class="nav-category__name">Consoles</p>
所以我試着這樣做:
elem = driver.find_element_by_xpath("//*[@id="1"]/p")
elem.click()
我發現這個XPath通過檢查元素和複製xpath,但它說「無法定位元素」。我怎麼能這樣做?
我不太瞭解HTML,但我可以通過id或名稱查找其他元素。
檢查此elem = driver.find_element_by_xpath(「// * [@ id ='1']/p」) –