set text of input element with python selenium - python

hi i have a element like below:
<input id="txt_search" class="search-box tp-co-1 tp-pa-rl-5 tp-re tp-bo-bo" type="text" placeholder="جستجوی سهم" onmouseup="this.select();" autocomplete="off">
I want to set text of this element to something
so I use below code
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "txt_search")))
browser.execute_script("arguments[0].setAttribute('value','something')", element)
so then it should appear a dropdownlist like below but it doesnot
<div id="list_dropdown"><div isin="IRO1PIAZ0001" symbol="غاذر1 - کشت‌وصنعت‌پیاذر"><span></span><span style="color: red;">غاذر</span><span>1 - کشت‌وصنعت‌پیاذر</span></div></div>
so then i must select first item of this dropdownlist
this is what i want

You have most of the code you need, but there's no reason to use browser.execute_script unless the standard element.send_keys function does not work as expected. Use the below code which leverages your existing code and send_keys function:
element = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, "txt_search")))
element.send_keys("something")

Related

partial text in xpath

<a class="ui-autocomplete-row ui-corner-all"
aria-label="(1100)Texas (PL1200/PC1030)- (200167)-Supplies: Other Supplies (620038): 1100-1200-200167-620038"
id="ui-id-7" tabindex="-1">
(1100)- (PL1200/PC1030)-(200167)-Supplies:
Other Supplies (620038):
<u><b>1100-1200-200167-620038</b></u>
</a>
In Xpath, I want to check if aria-label contains the value "1100-1200-200167-620038". It looks like there is a syntax error in the below statement:
element = wait.until(EC.visibility_of_element_located((By.XPATH,'//*[#aria-label="1100-1200-200164-620038"]')))
You can use two option like this:
CSS Selector
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'a[aria-label*="1100-1200-200167-620038"]')))
Xpath:
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//a[contains(#aria-label, "1100-1200-200167-620038")]')))
Xpath can be a hassle sometimes, I recommend using the CSS selector method to accomplish this:
element = driver.find_elements_by_css_selector("[aria-label='1100-1200-200167-620038']")
You could them use the element in your visibility_of_element_located method.

Finding/locating a clickable text by xpath in div/span format

I used the following line to click the Availability Grid button, but it failed to locate the element.
Class Sarsa-button-content is used everywhere so, I added text together to make it unique. However, it couldn't find it. What am I missing?
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='sarsa-button-content']/span[text()='Availability Grid']"))).click()
<div class="sticky-top-wrapper" style="top: 80px;">
<div class="site-filter-container" id="site-filter-container">
<a data-component="Button" class="sarsa-button view-by-availability-grid--button-tracker sarsa-button-primary sarsa-button-sm" href="/site/123456/availability">
<span class="sarsa-button-inner-wrapper">
<span class="sarsa-button-content">Availability Grid</span>
your xpath is wrong, try this:
//span[contains(#class, 'sarsa-button-content') and text() = 'Availability Grid']

How to click css selector by text using selenium python

I would like to test some actions. In this case, I need to click on the text to get an upload form, but none of the forms below do not lead to the desired result.
This part of HTML-code, where I need to do some actions:
<a href="javascript:;" class="dg-hider st-mb__20 st_add-material__link" onclick="iu.ajax(event,'https://alexanderro.com/ajax/documentAddGsiUpload');">
Choose your category
</a>
<div id="GSIData"></div>
none of these works:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "dg-hider st-mb__20 st_add-material__link"))).click()
driver.find_element_by_class_name("dg-hider st-mb__20 st_add-material__link").click()
driver.find_element_by_id("GSIData").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "GSIData"))).click()
driver.find_element_by_css_selector("a[onclick*=https://alexanderro.com/ajax/documentAddGsiUpload]").click()
Could you help me?
You need to wait for the element for its presence or visibility:
You can use the below code snippet, it should work
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element)))
element = "a.dg-hider.st-mb__20.st_add-material__link"
wait_for_element_to_be_clickable(element)
driver.find_element_by_css_selector(element).click()
print("Clicked")
I can't make a comment in Stack Overflow, so I'll try help you throug here:
Did you try:
driver.find_element_by_css_selector('[class="dg-hider st-mb__20 st_add-material__link"]').click()
or
driver.find_element_by_xpath('"//a[contains(text(), 'Choose your category')]"').click()

Alternatives to using selenium XPATH to find an element?

On this website, I'm trying to find an element based on its XPATH, but the XPATH keeps changing. What's the next best alternative?
Snippet from website
<button class="KnkXXg vHcWfw T1alpA kiPMng AvEAGQ vM2UTA DM1_6g _-kwXsw Mqe1NA SDIrVw edrpZg" type="button" aria-expanded="true"><span class="nW7nAQ"><div class="VpIG5Q"></div></span></button>
XPATH:
//*[#id="__id15"]/div/div/div[1]/div[2]/div
#Sometimes id is a number between 15-18
//*[#id="__id23"]/div/div/div[1]/div[2]/div
#Sometimes id is a number between 13-23
Here's how I use the code:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, """//*[#id="__id3"]/div/div/div[1]/div[2]/div/div/div/button"""))).click()
I've tried clicking the element by finding the button class, but for whatever reason it won't do anything.
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "KnkXXg vHcWfw T1alpA kiPMng AvEAGQ vM2UTA DM1_6g _-kwXsw Mqe1NA SDIrVw edrpZg"))).click()
If Part of the text is keep changing you can use contains in the xpath.
//*[contains(#id,"__id"]/div/div/div[1]/div[2]/div

How to click on radio button(I read previous questions) python?

I have the code
<div>
<input type="radio" id="grupo1" name="grupo1" name="ControlGroupSearchView2$AvailabilitySearchInputSearchView2$RadioButtonMarketStructure" value="OneWay" />
<label>One-way</label>
</div>
And I tried what other suggested to use
args["OW"] = "input[type='radio'][value='OneWay']"
WebDriverWait(self.browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, args["OW"])))
self.browser.find_element_by_css_selector(self.args["OW"]).click()
but I get error:
selenium.common.exceptions.TimeoutException: Message:
since it doesn't find the object.
Assuming there are no iframe elements on the page.
What I found working for radiobuttons is to click the corresponding label and not the input:
wait = WebDriverWait(self.browser, 10)
one_way = wait.until(EC.visibility_of_element_located((By.XPATH, "//label[. = 'One-way']")))
one_way.click()

Categories