Scroll the exact element into view_selenium python - python

I use a python script to perform some actions on a website. One of the actions is to insert value and select the exact value from the list. The issue with the code below is that is selecting the first value that appears and not the exact value.
For example, I'm searching for 'algodón' and it should select the exact value available in the dropdown as 'Algodón' and not 'Mezcla de algodón'.
my code
button = WebDriverWait(driver, 6).until(ec.element_to_be_clickable((By.CLASS_NAME,'awsui-select-dropdown-filter')))
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
ActionChains(driver).move_to_element(button).send_keys(a).perform()
# ------------------click checkbox
element = driver.find_element_by_class_name('awsui-select-option-checkbox')
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
bulk = WebDriverWait(driver, 5).until(ec.presence_of_element_located((By.XPATH,'//*[#id="awsui-tabs-0-FILTERED_QUESTIONS_TAB-panel"]/span/div/awsui-cards/div/div[2]/div[1]/span/span/div[1]/div/div/div[3]/awsui-button')))
ActionChains(driver).move_to_element(bulk).click(bulk).perform()
How this appears in the dropdown:
What should select:
Do you have any suggestion on how should I modify the code to select the exact match?

Related

Switch (internal) tab using Selenium

I've been trying to access elements of a website using Selenium but keep getting this message :
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
The problem I'm having seems to be that the website uses the same classes' name between two tabs, and just changes the visibility of one to display the other. For instance, when the first tab is displayed the first div's visibility is visible and the second is hidden. If the second tab is displayed, then the second div is visible, and the first hidden.
For the first tab, I have no problem accessing the desired elements. However, if the second tab is displayed, I get the above error message.
Is there a way to switch between to div like one would do between two iframe ?
This is what my code looks like for the moment :
driver.get(url) #get url
driver.find_element(By.XPATH, "//input[#id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_MotsClesTextBox']").send_keys(string) #enter searched term
driver.find_element(By.XPATH, "//input[#id='ctl00_cphMaster_ConsultationCitoyenCriteresRecherche_CritereRechercheTabContainer_RechercheSimpleTabPanel_RechercheSimpleControl_PorteeRadioButtonList_1']").click()
driver.find_element(By.XPATH, "//input[#class='Bouton BoutonRechercher']").click() #click the search button
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//div[#class='Header']")))
driver.find_element(By.XPATH, '//*[#id="__tab_ctl00_cphMaster_ConsultationCitoyenResultatsRecherche_ResultatsRechercheTabContainer_RechercheLETabPanel"]').click() #switch tab
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//div[#class='Header']")))
while True:
listings = driver.find_elements_by_class_name("InscriptionSummaryValue")
c = 1
for i in listings:
page = int(driver.find_element(By.XPATH, "//span[#class='CurrentPage']").text) #can't find the element because it seems to be located in the first div

I'm unable to locate the element to perform actions on it

I'm facing an issue locating the element on screen when there are no unique identifiers like ID, text etc. As it opens URL, I need to scroll down and click on button - 'Get Started' to proceed!...
Below is my code:
global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://My URL")
driver.implicitly_wait(10)
screen = driver.find_element(By.XPATH, '//div[#class="swiper-wrapper"]')
screen.click() (- This step doesnt through any error, i tried using this to scroll down the screen)
element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]')
driver.execute_script("arguments[0].scrollIntoView(true);", element )
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(),"Get Started")]'))).click()
or
element.click()
Please help me in determining how to locate the element.
enter image description here
In this case you are trying to find span which is inside #shadow-root, Selenium won't be able to click elements inside #shadow-root in easy way. Look here: How to click button inside #shadow-root (closed) using Selenium and Python
But in your case you probably don't need/want to click this specific span, becouse you have custom element ion-button, which has some click event listener attached to it.
You can check XPATH value in browser devtools. If you go to Elements tab, you can right click desired element and choose option Copy>Copy Xpath. Also make sure your element is not inside iframe tag, it could cause problem as well.

Web-scraping with selenium python

I'm trying to scrape a page whose classes are in this format class="jss262 jss434 jss263"
I tried to click on a box and insert a specific value but whether I use class, xpath or css selector, the element is not identified.
I believe I missed something but I'm not sure what it is. Is there a way to modify my code in order to access this element?
My code:
try:
button2 = WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.CLASS_NAME, 'jss262 jss434 jss263')))
button2.click()
button2.send_keys(att)
driver.implicitly_wait(5)
By.CLASS_NAME only accepts one class, use css_selector:
button2 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '.jss262.jss434.jss263')))

Not able to select and click dropdown search query result with Selenium webdriver

Im trying to perform select and click action from the search box result dropdown for testing purpose. Though i dont get ant error but i'm stuck and not able to do so, search results came then disappeared immediately. Please any one help me out. Im using Python script to automate webdriver. Here is the screenshot below for reference.
. I have tried webdriverwait for same action but it gives Timeout exception error. If there is any child actions from CSS to perform let me know. Here is what i tried
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.presence_of_element_located((By.XPATH, "//li[text()='flowers']")))
Apart from this snippet, i want to rather just perform enter key operation, where i get query result for 'flower' on this ecomm. website.
Here is the website URL- https://paytmmall.com
Once you type flower in the input field, there are multiple options appearing based on the input provided. They are in li tags and under b tag.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://paytmmall.com/")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.XPATH, "(//li)[4]/descendant::b[contains(text(),'flowers')]"))).click()
time.sleep is just for visibility purpose. you can remove that as well.
Also this xpath (//li)[4]/descendant::b[contains(text(),'flowers')] is based on xpath indexing , since I think you wanna select the 4th option which is flower itself. In case you wanna select a different option, you would have to write the different xpath.
In case you are looking to just select the searched item, it's better to pass enter key once you type flower in the input field.
You can use the below code for that :
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
search.send_keys(Keys.RETURN)
The suggested options are not containing the text directly in li elements, they are inside child elements inside li elements.
Try this instead:
search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")
dropdown = WebDriverWait(driver, 4).until(
EC.visibility_of_element_located((By.XPATH, "//li//*[text()='flowers']")))

Selecting an item from drop down menu when the menu uses input, not select

I'm trying to learn how to use Selenium with Python. I'm writing some code that will run a search on www.kijiji.ca. I'm able to select the search field and enter my query, but I can't figure out how to select the city from the list. In the Selenium documentation, I found where it says to use:
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
However, when I try to use the code, I get an error that says "Select only works on select elements, not on input"
I inspected the page again and it does seem like the drop down menu uses an input rather than a select. Can anyone help me figure out how to use Selenium to select the city here?
You can use Select class if your element is a <select> tag. Refer seleniumhq, there it mentionedModels a SELECT tag, providing helper methods to select and deselect options.
Specific to you element, you can try below code as manually we simulate the action like click.
driver.find_element_by_id("SearchLocationPicker").click()
driver.find_element_by_css_selector("li a[title='Manitoba']").click()
driver.find_element_by_css_selector("li a[title='Flin Flon']").click()
Working fine at my end. let me know if you have any query.
The page uses custom select component which cannot be interacted with selenium Select class. The Select class can be used only for default html select component.
We have to automate it as we do in the manual steps,
Click the input test box
Wait for the dropdown list appears.
Click the value respective value appears in the the dropdown.
In your case, we can select the dropdown either with title attribute or by selecting display value of the anchor tag in the list.
<a title="Territories">Territories</a>
def select_by_title(value):
# click the input component
driver.find_element_by_css_selector('input#SearchLocationPicker"]').send_keys(value)
# Wait for the value to appear in dropdown.
wait = WebDriverWait(driver, 60)
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#SearchLocationPicker~div>ul>li>a[title="'+value+'"')))
# Click the element
element.click()
Or
def select_by_display_value(value):
driver.find_element_by_css_selector('#SearchLocationPicker"]').send_keys(value)
wait = WebDriverWait(driver, 60)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[#id="SearchLocationPicker"]/following-sibling::div/ul/li/a[text() = "'+value+'"]')))
element.click()

Categories