Python 无法使用Selenium在搜索字段中输入文本
设立: 我在一家网店的在线后端编辑产品 我必须更新数百种产品的价格,并希望通过脚本这样做 我已使用Selenium成功登录到系统,并导航到产品管理页面,该页面包含一个包含产品的表 问题: 我似乎无法摆弄这张桌子。有一个高级搜索,似乎是生活在桌子上 我尝试通过XPath或CSS选择器在搜索字段中输入文本,但似乎没有任何效果 Python执行代码时不会出错,但文本不会出现在搜索字段中 通过XPath:Python 无法使用Selenium在搜索字段中输入文本,python,css,selenium,xpath,Python,Css,Selenium,Xpath,设立: 我在一家网店的在线后端编辑产品 我必须更新数百种产品的价格,并希望通过脚本这样做 我已使用Selenium成功登录到系统,并导航到产品管理页面,该页面包含一个包含产品的表 问题: 我似乎无法摆弄这张桌子。有一个高级搜索,似乎是生活在桌子上 我尝试通过XPath或CSS选择器在搜索字段中输入文本,但似乎没有任何效果 Python执行代码时不会出错,但文本不会出现在搜索字段中 通过XPath: search_field = browser.find_element_by_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
使用类名作为定位器
search_field= browser.find_element_by_class_name('text-field text-field-250 searchBox')
search_field.send_keys('brand')
search_field= browser.find_element_by_xpath("//input[@class='text-field text-field-250 searchBox']")
search_field.send_keys('brand')
或者使用xpath作为定位器
search_field= browser.find_element_by_class_name('text-field text-field-250 searchBox')
search_field.send_keys('brand')
search_field= browser.find_element_by_xpath("//input[@class='text-field text-field-250 searchBox']")
search_field.send_keys('brand')
尝试以下代码-在这里我使用了显式等待:
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
search_field = ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".text-field.text-field-250.searchBox")))
browser.execute_script("arguments[0].scrollIntoView(true);", search_field)
search_field.clear()
search_field.send_keys("brand")
PS:使用CSS选择器作为定位器。
希望它能帮助你 要在搜索字段中输入文本品牌,可以使用以下代码块:
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')
您可以显示搜索字段的HTML吗?似乎需要在使用该输入执行操作之前添加等待。如果只有one@LucSpan,试试我的答案。谢谢,但是它给出了一个NoTouchElementException:无法定位元素:。文本字段text-field-250 searchBoxtry with xpath onceOk让我再次检查它一旦它是一个框架,它就可以正常工作了。我的结尾似乎是与iframe或其他一些相关的问题。请提供完整的HTMLhanks,但搜索字段中没有显示文本:-也没有错误消息。@LucSpan,共享您的链接或扩展的HTML。似乎有一个iframe。您现在添加的HTML可能太多了。@LucSpan,页面上有iframe标记吗?