My website form have updated and my script is no more working. I cannot fix it because I'm not able to find how to select in a dropdown content using selenium chrome web driver and python.
formattedstate is the value of a formatted data (read from a file)
driver.find_element_by_css_selector(f"option[title='{formattedState}']").click()
driver.find_element_by_xpath("//*[text()='{formattedState}']").click()
driver.find_element_by_css_selector(f"option[value='{formattedState}']").click()
This is the data of the dropdown content from the webform. I selected the first state in the dropdown = Alabama
<input aria-valuetext="" role="combobox" tabindex="0"
placeholder="State/Province/Region" readonly="" height="100%" size="1" autocomplete="off" value="" data-spm-anchor-id="a2g0o.placeorder.0.i14.5f0b321eC7zfLx">
<li class="next-menu-item" title="Alabama" data-spm-anchor-id="a2g0o.placeorder.0.i17.5f0b321eC7zfLx">Alabama</li>
It should select the correct state in the dropdown content
First try to click to the combobox, then wait until state option(li) element is visible and click.
In code below, I used css selector to get li by title. If you want to find element by text, use:
wait.until(ec.visibility_of_element_located((By.XPATH, f"//li[.='{state}' and #class = 'next-menu-item']"))).click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
state = "Alabama"
driver.find_element_by_css_selector('input[placeholder="State/Province/Region"]').click()
wait.until(ec.visibility_of_element_located((
By.CSS_SELECTOR, f"li.next-menu-item[title='{state}']"))).click()
JS SetAttribute works surprisingly well for Combobox. Try
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//id of element').setAttribute('value', '//value to set')");
// to click
js.executeScript("arguments[0].click();", element);
Related
been dealing with a countries dropdown using Selenium Python;
here is the Div tag of the drop-down menu:
<div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c95-29"><div class="mat-select-value ng-tns-c95-29" id="mat-select-value-1"><span class="mat-select-placeholder mat-select-min-line ng-tns-c95-29 ng-star-inserted"></span><!----><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-29"><div class="mat-select-arrow ng-tns-c95-29"></div></div></div><!---->
and here is the Div tag for the targeted option:
<div class="mat-select-value ng-tns-c95-29" id="mat-select-value-1"><!----><span class="mat-select-value-text ng-tns-c95-29 ng-star-inserted" style=""><span class="mat-select-min-line ng-tns-c95-29 ng-star-inserted">MAROKO</span><!----><!----></span><!----></div><div class="mat-select-arrow-wrapper ng-tns-c95-29"><div class="mat-select-arrow ng-tns-c95-29"></div></div>
and here's the specific Span Tag for the targeted option
<span class="mat-select-min-line ng-tns-c95-29 ng-star-inserted">MAROKO</span><!----><!---->
the code used to select an option from that dropdown is :
Country=browser.find_element(By.ID, 'mat-select-value-1')
time.sleep(5)
drop=Select(Country)
time.sleep(5)
drop.select_by_visible_text("MAROKO")
output
Exception has occurred: UnexpectedTagNameException
Message: Select only works on <select> elements, not on <div>
File "C:\Users\test\form.py", line 31, in <module>
drop=Select(Country)
I went with drop.select_by_visible_text since I believe it's the only available option!
Imports used
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time
I would appreciate any helpful comment.
Cheers
Tried this
specifcly this;
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id="mat-radio-2"]/label/span[1]/span[1][text()='MAROKO']"))).click()
but it seems that there's a syntax error
You can use the selenium's Select() class only for html <select> tags. But this dropdown is implemented with <div>, so you can handle it just like html elements.
First you need to click the dropdown to expand the options and then click the option you need:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[#class="mat-select-trigger ng-tns-c95-29"]'))).click()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'mat-select-value-1'))).click()
I've created the locators using the html parts you provided. Perharps you will need to update them.
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.get("https://accounts.ebuyer.com/customer/account/index.html?action=bG9naW4=")
title = driver.find_element_by_name("email")
title.clear()
title.send_keys("johnsmith#gmail.com")
password = driver.find_element_by_id("password")
password.send_keys("pass123")
driver.find_element_by_xpath('//*[#id="login"]/div[4]/input').click()
driver.find_element_by_xpath('/html/body/div[3]/div/button[2]').click()
#accepts cookies
driver.get("https://www.ebuyer.com/search?q=graphics+cards")
WebDriverWait(driver, 10)
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH,'//*[#id="grid-view"]/div[1]/div[4]/button'))
) #finds add to basket button
element.click()
driver.find_element_by_xpath('/html/body/header/div/div[3]/div/a[1]').click()
print("IN BASKET")
#adds to basket if found
except:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH,'//*[#id="main-
content"]/div/div[1]/div[2]/div[2]/div/div[2]/ul/li[2]/a'))
)
element.click()
#moves to next page
this is what I'm trying to click but the id changes on each product grid.
<button class="button button--mini-basket button--small js-add-to-mini-basket" data-product-
id="798879" data-ajax-basket-url="https://orders.ebuyer.com/customer/products/index.html?
action=YWpheF9jYXJ0X2FkZF9pdGVt" data-analytics-event="click" data-event-category="Search Listings - Grid" data-event-action="Product Click" data-event-label="Add to Basket Button">Add to
Basket</button>
I want to take <button class="button button--mini-basket button--small js-add-to-mini-basket" and
use this instead of By.XPATH,'//*[#id="grid-view"]/div[1]/div[4]/button'
I'm trying to scan the full page to find the Add to Basket Button and if it can't find that then it
will move on to the next page and repeat until it is found.
I have just built my first pc and I cannot get a graphics card anywhere so I want to use this grab
one.
I've recently found a love for coding so I'm quite new to this, I hope I've explained myself properly
and any help would be appreciated.
Use following xpath to identify. Use element_to_be_clickable() instead presence_of_element_located()
element =WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Add to Basket']"))
Try following xpath
//button[#class='button button--mini-basket button--small js-add-to-mini-basket' and contains(text(),'Add to Basket')]
Basically I want to automate google search engine which basically search and click on the first link that is populated after search button is clicked so i am using python do this. i am successfully able to search the things but not able to click on the link after page reload
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
url = "https://www.google.com"
driver = webdriver.Chrome("C:/Users/User/Python/AllCodes/chromedriver.exe")
driver.get(url)
insert = driver.find_element_by_name("q")
insert.send_keys("K.J.Somaiya")
button = driver.find_element_by_name("btnK")
button.submit()
# after this new page reload and link are poluated
linktext = driver.find_element_by_link_text("K. J. Somaiya Institute of Management")
linktext.submit()
This input class i used name which is q
<input class="gLFyf gsfi" jsaction="paste:puy29d;" maxlength="2048" name="q" type="text" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwj9qOfEmLzvAhWSj-YKHZUYDngQ39UDCAQ">
This the html code from i target linktext which is K.J.Somaiya Inst.....
you need to wait until that element becomes visible in dom. Read about selenium waits here
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://www.google.com"
driver = webdriver.Chrome("C:/Users/User/Python/AllCodes/chromedriver.exe")
driver.get(url)
insert = driver.find_element_by_name("q")
insert.send_keys("K.J.Somaiya")
button = driver.find_element_by_name("btnK")
button.submit()
# after this new page reload and link are poluated
linktext = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "K. J. Somaiya"))
) # Waits 10 second before element loads.
linktext.click()
I need to select a dropdown value , I checked the code it doesnt contain select class. Is there any way we can select the value without select class
By this I am clicking on dropdown which then enables the dropdown values
driver.find_element_by_xpath("(//div[#class='ot-lookup__input-container'])").click()
That is the dropdown
The code for Xpath is : (**it is same for every value)
<button _ngcontent-fru-c1=""
class="slds-media slds-listbox__option slds-listbox__option_entity slds-listbox__option_has-meta tabbable-button"
role="option"
type="button"
id="listbox-option-unique-id-[object Object]">
<!----><!---->Skipped<!---->
</button>
You can. Click to open dropdown and then click on required option (in your case button). In code below I used WebDriverWait to wait until the button with text Skipped will be clickable:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ...
wait = WebDriverWait(driver, 10)
# ...
driver.find_element_by_xpath("(//div[#class='ot-lookup__input-container'])").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Skipped']"))).click()
I am trying to scrape the following website using Python 3, Selenium, and PhantomJS:
https://health.usnews.com/best-hospitals/search
I need to locate a search field and enter text into it, and then press enter to generate the search results. Below is the HTML that corresponds to the search field I am trying to locate:
<div class="search-field-view">
<div class="block-tight">
<label class="" for="search-facet-city">
<input id="search-facet-city" autocomplete="off" name="city"
type="text" data-field-type="text" placeholder="City, State or ZIP"
value="" />
</label>
</div>
</div>
Below is my Python 3 code that attempts to locate this search field using the id "search-facet-city."
def scrape(self):
url = 'https://health.usnews.com/best-hospitals/search'
location = 'Massachusetts'
# Instantiate the driver
driver = webdriver.PhantomJS()
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)
elem = driver.find_element_by_id("search-facet-city")
elem.send_keys(self.location)
driver.close()
I need to scrape some results from the page once the text is entered into the search field. However, I keep getting a NoSuchElementException error; it is not able to locate the search box element despite the fact that it exists. How can I fix this?
I tried this with Chrome:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
url = 'https://health.usnews.com/best-hospitals/search'
location = 'Massachusetts'
# Instantiate the driver
driver = webdriver.Chrome(executable_path=r'/pathTo/chromedriver')
#driver = webdriver.PhantomJS(executable_path=r'/pathTo/phantomjs')
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
driver.save_screenshot('out.png');
elem=wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='search-field-view']")))
span = elem.find_element_by_xpath("//span[#class='twitter-typeahead']")
input=span.find_element_by_xpath("//input[#class='tt-input' and #name='city']");
input.send_keys(location)
driver.save_screenshot('out2.png');
and it works.
But if I try with phantomJS, in driver.save_screenshot('out.png'); I obtain:
As #JonhGordon said in the comments, the website make some checks. If you want to use phantomJS, you could try to change the desired_capabilities or the service_args.