硒隐藏/包裹元素\列表

时间:2021-05-07 09:39:42

标签: python list selenium-webdriver

我一直在试图理解为什么下面的代码不起作用。我打算向位于不同 Web 选项卡上的搜索输入发送一个键,然后按下一个按钮。在论坛中阅读类似的 questions 后,我认为该元素可能被隐藏或包裹。如何找到这个?此外,有时元素在列表中,必须通过索引访问。我研究过的例子不是那样的。任何帮助将不胜感激。

from selenium import webdriver
from selenium.common.exceptions import *

 
webdriver_path = "C:/Users/escob/Documents/Projects/WebScrapingExample/chromedriver.exe"

magpie_url = 'https://www.musicmagpie.co.uk/start-selling/'
search_item = "9781912047734"

options = webdriver.ChromeOptions()
browser = webdriver.Chrome (webdriver_path, options = options)
browser.get(magpie_url)

sell_tab = browser.find_element_by_id('pills-media-tab')
sell_tab.click()

##I have tried the following code with no luck
#search_bar = browser.find_elements_by_name('searchString')
#search_bar = browser.find_elements_by_class_name('form-input')
search_bar = browser.find_element_by_xpath("//input[@name='searchString']")

#I am getting elements in a list, the examples I have seen do not need indexing
search_bar[0].send_keys(search_item)


button = browser.find_element_by_class_name(submit-media-search) #will this work?
button[0].click()  #again in a list?

非常感谢您对 Seleniumista 初学者的帮助。

1 个答案:

答案 0 :(得分:1)

我不能 100% 确定您要通过搜索完成什么,因此我只能提供有关搜索字符串的指导 9781912047734。下面的代码将输入搜索字符串并单击添加按钮。

我注意到页面有一个“接受 cookie 按钮”,所以我添加了代码来绕过这个。

如果此代码对您有帮助,请告诉我。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")

# Hide the "Chrome is being controlled by automated test software" banner
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
url = 'https://www.musicmagpie.co.uk/start-selling'
response = driver.get(url)
driver.implicitly_wait(15)

hidden_element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.CLASS_NAME, "cookieBar")))
if hidden_element.is_displayed():
    driver.implicitly_wait(30)
    driver.find_element_by_link_text('Accept all cookies').click()
else:
   pass


tab = driver.find_element_by_id('pills-media-tab')
tab.click()

# this implicitly_wait is waiting for the page to fully load
driver.implicitly_wait(60)

# this xpath can likely be refined 
enter_barcode = driver.find_element_by_xpath("*//div[3]/div/form/div/div[1]/div[1]/input")
enter_barcode.send_keys("9781912047734")

# waiting for the keys to be sent
driver.implicitly_wait(20)

add_button = driver.find_element_by_css_selector("div.form:nth-child(19) > "
                                                  "div:nth-child(1) > form:nth-child(1) > "
                                                  "div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > "
                                                  "input:nth-child(1)")
add_button.click()

# do something else

# call close when finished
driver.close()

相关问题