2

I'm trying to input the text string "L62T18H029-P3215" into the search input box on this website https://lamerfashion.com and press Enter.

I have tried to execute some javascript to change the value of the hidden element however I am unable to make Selenium send the ENTER key to submit.

driver = webdriver.Chrome(ChromeDriver)

driver.get("https://lamerfashion.com")

element = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, '//a[@class="search-icon"]')))

element.click()

e = driver.execute_script("return document.getElementsByName('type')[0].value;")

print(e)

driver.execute_script("document.getElementsByName('type')[0].value='L62T18H029-P3215';")

e = driver.execute_script("return document.getElementsByName('type')[0].value;")

print(e)

Output:

product

L62T18H029-P3215

3 Answers 3

4

I run script in java, maybe this will help you..Try this(for Reference)

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://lamerfashion.com");

WebElement newSearch = driver.findElement(By.className("search-icon")););
newSearch.click();
Thread.sleep(1000);
WebElement searchpro = driver.findElement(By.xpath("//*[@id=\"navbar\"]/div/ul[2]/li[1]/form/input[2]"));

searchpro.sendKeys("L62T18H029-P3215");
searchpro.sendKeys(Keys.ENTER);
Sign up to request clarification or add additional context in comments.

5 Comments

Your Xpath looks horrible. It's highly unstable also. Though OP is asking help in python. There is no need of TAB also.
ok i will edit code but I already mention 'I run script in java' and for logic I gave answer.
I agree with @PradnyaBolli 's answer except the xpath part. It is not at all required to use JS to fiddle around the hidden element. Just clicking on the search icon starts displaying the search field input box where sendKeys can be done effortlessly.
@PradnyaBolli You need to drop the Thread.sleep() and suggest inducing WebDriverWait
@DebanjanB yes you are right..I read about 'which is better to use Thread or WebDriverWait?' and 'WebDriverWait' is always better to use.Thanks for suggestion..
2

I don't see any need of JS here.
You can simply go ahead with send_keys method which is already present in selenium.

Code :

driver = webdriver.Chrome(executable_path = r'chromedriverpath')
wait = WebDriverWait(driver,10)

driver.maximize_window()

driver.get("https://lamerfashion.com")

wait = WebDriverWait(driver, 10)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.search-icon'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='search-icon']/following-sibling::form/input[@name='q']"))).send_keys("L62T18H029-P3215")  

imports :

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

2 Comments

This works ^^. Can you please explain to me what the "/following-sibling::form" part of the xpath does ?
Following-sibling means you want to select following node from the current node with in the same parent.
0

Seeing the website, there are two inputs and the one with hidden might actually not be the one you need.

Try the same thing but for the name "q"

Moreover, try using Selenium command

element.send_keys('text_you_want') - that should be sufficient istead of the JS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.