如何解决硒蟒中嵌套循环的问题



我的循环遇到了一些问题。我有两个变量可以导航到一个表的不同子项(两个变量都是元素列表(。第一个变量导航到名称,第二个变量导航到输入。当第一个变量中的元素名称等于某个单词时,我想单击特定输入。问题是我的第二个(嵌套(循环从第一条记录开始,而它应该迭代直到没有条件。感谢您的建议。我是硒的新手,所以也许网络元素的工作方式与其他东西不同。

sel = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"table.table.table-condensed.table-top-spacing td[valign='top']")))
sel_input = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"table.table.table-condensed.table-top-spacing input.nightlife_drug_quantity")))
for s in sel:
for i in sel_input:
if s.text == "LSD":
i.click()
break

.HTML

<table class="table table-condensed table-top-spacing"><thead><tr><th width="70%">Drug</th> <th width="10%">Stamina</th> <th width="10%">Price</th> <th width="20%">Buy</th></tr></thead> <tbody><tr><td valign="top">Painkillers</td> <td>1%</td> <td>$23</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Booze</td> <td>2%</td> <td>$23</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Weed</td> <td>1%</td> <td>$19</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Magic mushrooms</td> <td>2%</td> <td>$27</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">LSD</td> <td>3%</td> <td>$15</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Ecstasy</td> <td>4%</td> <td>$15</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Cocaine</td> <td>7%</td> <td>$62</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Opium</td> <td>6%</td> <td>$37</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Amphetamine</td> <td>5%</td> <td>$70</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Heroin</td> <td>8.5%</td> <td>$43</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Hash</td> <td>2.5%</td> <td>$16</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Special K</td> <td>7.4%</td> <td>$55</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">Morphine</td> <td>7.9%</td> <td>$46</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr><tr><td valign="top">GHB</td> <td>3.6%</td> <td>$22</td> <td nowrap="nowrap"><input name="quantity" type="text" maxlength="2" class="nightlife_drug_quantity"> <button class="btn btn-inverse btn-small">Buy</button></td></tr></tbody></table>

你不需要两个 for 循环。使用xpathfollowingtd 或following-siblingtd 都可以工作。请尝试以下代码。

sel = WebDriverWait(browser, 20).until(EC.presence_of_all_elements_located((By.XPATH,"//table[@class='table table-condensed table-top-spacing']/tbody//tr//td[@valign='top']")))
for s in sel:
if s.text=='LSD':
s.find_element_by_xpath('./following::td/input[@name="quantity"]').click()
s.find_element_by_xpath('./following::td/input[@name="quantity"]').send_keys("12")
break;

最新更新