gpt4 book ai didi

python - 无法使用 Selenium 在搜索字段中输入文本

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:02 25 4
gpt4 key购买 nike

设置:

我正在网上商店的在线后端编辑产品。

我必须更新数百种产品的价格,并且想通过脚本来完成。

我已经使用 Selenium 成功登录到系统并导航到产品管理页面,该页面由包含产品的表格组成。


问题:

我似乎无法让 selenium 操纵表格。有一个高级搜索似乎已经摆在桌面上。

我尝试通过 XPath 或 CSS 选择器在搜索字段中输入文本,但似乎没有任何效果。

Python 执行代码没有错误,但文本不会出现在搜索字段中。

通过 XPath:

search_field = browser.find_element_by_xpath(
'/html/body/table/tbody/tr[2]/td[4]/table[2]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[1]/td/div/input',
)
search_field.clear()
search_field.send_keys('brand')

通过 CSS 选择器:

search_field = browser.find_element_by_css_selector(
'#Products > tbody > tr:nth-child(1) > td > div > input',
)
search_field.clear()
search_field.send_keys('brand')

如何将文本放入搜索字段?

搜索字段的 HTML 是:

<input type="text" class="text-field text-field-250 searchBox">

HTML

<tr>
<td class="searchArea">
<div class="grid-area-filter-item">
<label class="line-label">Search:</label>
<input type="text" class="text-field text-field-250 searchBox">
</div>
<span class="grid-area-filter-item">
<a href="#" class="button button-fleft searchButton">
<span>Search</span>
</a>
<a href="#" class="button button-fleft resetButton">
<span>Reset</span>
</a>
</span>
<a class="link-advanced">Advanced</a>
<table class="advanced-search" style="display: none;">
<tbody>
<tr>
<td class="label">Type:
</td>
<td class="control">


<select class="select-400" data-val="true" data-val-number="The field ProductTypeId must be a number." id="ProductTypeId" name="ProductTypeId"><option value="">Please select</option>
<option value="2">Accessories</option>
<option value="3">Back Cushion</option>
<option value="4">Down-Blend</option>
<option value="6">Foot</option>
<option value="7">Foot Finish</option>
<option value="8">Leather Care</option>
<option value="9">Mattress</option>
<option value="10">Mattress Upgrade</option>
<option value="11">Power Reclining</option>
<option value="12">Seat Cushion</option>
<option value="13">Sleeper Sofa Sheets</option>
<option value="14">Sleeper</option>
<option value="15">Stressless Promo Accessories</option>
<option value="16">Enso Foundation</option>
<option value="17">Stand Alone Mattresses</option>
<option value="18">Base</option>
<option value="19">Enso Pillows</option>
<option value="20">Ottoman</option>
<option value="21">Add Pillows</option>
<option value="22">Rug</option>
<option value="23">Swivel</option>
<option value="25">Storage Chaise</option>
<option value="26">Office Chair</option>
<option value="27">Casters</option>
<option value="29">Paragon Sleeper Sheets</option>
<option value="30">Power Sleeper</option>
<option value="32">Bernhardt Nailheads</option>
<option value="33">Power + Swivel</option>
<option value="34">Bernhardt Tables</option>
<option value="35">Nailheads</option>
<option value="36">Base Type</option>
<option value="37">Wooden Side Panel</option>
</select>

</td>
</tr>
<tr>
<td class="label">Model:
</td>
<td class="control">


<div class="hide-input">
<input data-val="true" data-val-number="The field ModelId must be a number." id="ModelId" name="ModelId" type="text" value="">
</div>


<span class="button button-fleft"><input type="button" class="lookup-button" data-lookupurl="/models/lookup" data-hiddeninput="ModelId" value="Select"></span>
<label for="ModelId">Please select item</label>



</td>
</tr>
<tr>
<td class="label">Brand:
</td>
<td class="control">


<div class="hide-input">
<input data-val="true" data-val-number="The field BrandId must be a number." id="BrandId" name="BrandId" type="text" value="">
</div>


<span class="button button-fleft"><input type="button" class="lookup-button" data-lookupurl="/brands/lookup" data-hiddeninput="BrandId" value="Select"></span>
<label for="BrandId">Palliser</label>



</td>
</tr>
<tr>
<td class="label">Size:
</td>
<td class="control">


<div class="hide-input">
<input data-val="true" data-val-number="The field SizeId must be a number." id="SizeId" name="SizeId" type="text" value="">
</div>


<span class="button button-fleft"><input type="button" class="lookup-button" data-lookupurl="/sizes/lookup" data-hiddeninput="SizeId" value="Select"></span>
<label for="SizeId">Please select item</label>



</td>
</tr>
<tr>
<td class="label">Customization Level:
</td>
<td class="control">

<select class="select-400" id="CustomizationLevel" name="CustomizationLevel"><option value="">Please select</option>
<option value="Full">Full Customization</option>
<option value="Little">Little Customization</option>
<option value="No">No Customization</option>
<option value="Custom">Custom Customization</option>
<option value="MarriedGroup">Married Group Customization</option>
<option value="MarriedSale">Married Sale Customization</option>
<option value="PartlyInStock">Partly In Stock Customization</option>
<option value="Complex">Complex Customization</option>
<option value="NotForSale">Not For Sale</option>
</select>
</td>
</tr>
<tr>
<td class="label">Back Type:
</td>
<td class="control">

<select class="select-400" id="BackType" name="BackType"><option value="">Please select</option>
<option value="TightBack">Tight-Back</option>
<option value="AttachedPillowBack">Attached Pillow-Back</option>
<option value="LoosePillowBack">Loose Pillow-Back</option>
</select>
</td>
</tr>
<tr>
<td class="label">Furniture Type:
</td>
<td class="control">


<select class="select-400" data-val="true" data-val-number="The field FurnitureId must be a number." id="FurnitureId" name="FurnitureId"><option value="">Please select</option>
<option value="1">Fabric</option>
<option value="2">Leather</option>
</select>

</td>
</tr>
<tr>
<td class="label">
Out Of Stock Type:
</td>
<td class="control">

<select class="select-400" id="ProductAvailabilityType" name="ProductAvailabilityType"><option value="">Please select</option>
<option value="ZeroQuantity">Zero Quantity</option>
<option value="HiddenFromSite">Hidden From Site</option>
</select>
</td>
</tr>
</tbody>
</table>

</td>
</tr>

最佳答案

要在搜索字段中输入文本brand,您可以使用以下代码块:

search_field = browser.find_element_by_xpath("//td[@class='searchArea']/div[@class='grid-area-filter-item']//input[@class='text-field searchBox' and contains(@class,'text-field-')]")
search_field.clear()
search_field.send_keys('brand')

关于python - 无法使用 Selenium 在搜索字段中输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48784487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com