0

I have been trying to understand why the following code does not work. I am intending to send a key to a search input that is on a different web tab and then press a button. After reading similar questions in the forum I think the element might either be hidden or wrapped. How to find this? Also sometimes the elements are in a list and have to be accessed by indexing. The examples I have studied are not like that. Any help will be appreciated.

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?

Thank you so much for your help from a beginner Seleniumista.

1 Answers1

1

I'm not 100% sure what you're trying to accomplish with search, so I can only provide guidance on searching for your string 9781912047734. The code below will enter the search string and click the add button.

I noted that the page has an "accept cookies button", so I added the code to bypass this.

Please let me know if this code helps you.

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()

Life is complex
  • 15,374
  • 5
  • 29
  • 58
  • Thanks a lot for your answer. It was useful to learn about hidden_element. What I'm trying to achieve is to send keys to the search bar that is in the second tab "Sell CDs, DVDs, Games & Books"). I have managed to click on the tab by running this code sell_tab = driver.find_element_by_id('pills-media-tab') sell_tab.click() But I am unable to find the search bar and send the key. I have tried this: search_bar = driver.find_elements_by_name('searchString') search_bar[0].send_keys("9781912047734") button = driver.find_elements_by_class_name('submit-media-search') button.click( – Gustavo A Escobar-Palafox May 08 '21 at 10:43
  • 1
    So you want to input the search string on the "Sell CDs, DVDs, Games & Books"? Let me update my answer to do that. – Life is complex May 08 '21 at 10:56
  • @GustavoAEscobar-Palafox I added the code to input a barcode and add it to the basket. I didn't add the error handling code for any items that might not be accepted. You will need to do this. Please let me know if you have any questions. – Life is complex May 09 '21 at 13:59
  • Thanks a lot for your help. I have just tried the code and it is perfect. I learnt so much, thanks. I have added error handling so all is good. Thanks! – Gustavo A Escobar-Palafox May 12 '21 at 06:40
  • You're welcome. I'm glad that I could help you solve your problem. Please accept my answer. – Life is complex May 12 '21 at 12:28