Selenium Python selecting dropdown element - python

I'm having a lot of trouble finding the web element corresponding to the drop down box directly to the right of Code:. I've tried using Select, finding by tag_name, css_selector, xpath, name, class_name, select_by_visible_text, and haven't been able get it. I'm guessing the issue is that the dropdown box is nested within the form tag, highlighted in blue in the picture. The closest I've gotten is this:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#code_section_srch form#expertsearchformid select#expertsearchformid:toctextcodeid")))
Can you help me understand what I'm doing wrong?
Any insight would be greatly appreciated!

You have to use quotas, escape colon or use find by ID:
div#code_section_srch form#expertsearchformid select#expertsearchformid\:toctextcodeid
div#code_section_srch form#expertsearchformid select[id='expertsearchformid:toctextcodeid']
This should work:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "expertsearchformid:toctextcodeid")))
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[id='expertsearchformid:toctextcodeid']")))

Related

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.

Change to iframe

On gmx.net I want to click on a hyperlink in an email.
But the hyperlink is enclosed with two iframes.
How do I change into it?
My approach:
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class="app-stack_children--active"]')))
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class="js-box-flex need-overlay"]')))
As I suspected, your XPath is incorrect.
You have to either use
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#class='app-stack__children l-vertical app-stack__children--active']')))
or
WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[contains(#class, 'app-stack_children--active']')))
Obviously, the first option is better than the second one. There's always a chance that contains() will match more than one element. It is less probable if using equality.

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']")))

unique xpath for element span inside div inside button selenium python

Xpath
I'm looking for Unique Xpath for this element (Select)
in google language settings
https://myaccount.google.com/language?hl=en
already have this xpath but I need something more accurate and unique
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH,
"//*[#id='yDmH0d']/div[11]/div/div[2]/div[3]/div[2]/button"))).click()
If you pay attention to the HTMLDOM :
You can construct a xpath based on attribute name only. You do not need their values nor their text.
//div[#data-is-touch-wrapper]/button[#data-id and #aria-label]
represent two matching nodes.
(//div[#data-is-touch-wrapper]/button[#data-id and #aria-label])[2]
should get the job done.
I would try to copy the X-Path from the Browser as discribed here:
Mozilla Help
My result would be: //*[#id="lang-selector"]

Clicking first element of drop down menu that is not a SELECT element -Python selenium

I have a drop down menu that I haven't been able to figure out how to click the first element for. It is not a SELECT element, so I have been clicking the drop down, waiting for elements to be visible, and then try selecting the first option. That has not been working, and I'm not sure what I'm missing.
driver.get("https://bi.prozorro.org/sense/app/2595af2b-985f-4771-aa36-2133e1f89df0/sheet/48781d08-1fce-489b-af05-34c253e95ec2/state/analysis#view/pEh")
#click for menu to appear
tenderercode=WebDriverWait(driver, 25).until(EC.presence_of_element_located((By.XPATH,"//span[#class='title ng-binding']")))
tenderercode.click()
sleep(3)
#entering text in menu
element = driver.find_element_by_xpath("//input[#placeholder='Search in listbox']")
element.click()
element.send_keys("test")
#click on first element
first=WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"body.qv-client.qv-sheet-enabled.qv-view-sheet:nth-child(2) div.ng-scope.ng-isolate-scope div.lui-popover-container.ng-scope div.qv-listbox-popover.ng-scope.lui-popover div.lui-nopad.lui-popover__body.ng-scope div.listbox-wrapper.ng-scope div.qv-object-wrapper.ng-isolate-scope article.qv-object.qvt-visualization.qv-layout-xsmall.qv-object-listbox.qv-selections-active div.qv-inner-object div.qv-object-content-container:nth-child(4) div.qv-object-content.ng-isolate-scope div.qv-listbox-container.ng-scope.qv-listbox-interactive div.qv-listbox-wrapper.searchActive div.qv-listbox-scroll-area.qv-animate.ng-isolate-scope.qv-listbox-virtual-scroll-enabled div.scroll-content:nth-child(1) ul.qv-listbox.ng-scope > li.qv-listbox-item.ng-scope.serverOptional:nth-child(1)")))
first.click()
Error: selenium.common.exceptions.TimeoutException
The timeout error makes me think my css selector is incorrect, but I've tried the xpath too, and that hasn't fared much better. A little bit at my wits end, so any ideas are much appreciated.
Change:
first=WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "your locator")))
first.click()
to
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "your locator")))
first = driver.find_element_by_css_selector("your locator")
first.click()
And fix locator. It is not the main issue, but it's also the problem.
Update. I also find one more locator.
To find the first item use the locator use:
driver.find_element_by_css_selector(".qv-listbox-item.serverOptional:nth-of-type(1)>.qv-listbox-text>span")
To click use:
driver.find_element_by_css_selector(".qv-listbox-item.serverOptional:nth-of-type(1)>.qv-listbox-text>span>span")

Categories