A selenium newbie here. I'm trying to use Selenium to automate download of CSV file from calltools and upload it to salesforce. Here is the UI for the dropdown menu for selecting the options before exporting the CSV.
The HTML for the same is:
<div _ngcontent-rnw-c215="" style="">
<label _ngcontent-rnw-c215="">Templates</label>
<br _ngcontent-rnw-c215="">
<select _ngcontent-rnw-c215="" class="ng-pristine ng-valid ng-touched">
<option _ngcontent-rnw-c215="" value="0: null"></option>
<option _ngcontent-rnw-c215="" value="1: Agent KPI" class="ng-star-inserted"
style="" xpath="1">Agent KPI</option>
<option _ngcontent-rnw-c215="" value="2: Payroll" class="ng-star-
inserted">Payroll</option>
<option _ngcontent-rnw-c215="" value="3: Agent Status" class="ng-star-
inserted">Agent Status</option>
<!---->
</select>
I have tried multiple ways to access the appropriate option (1. Agent KPI). My code so far is
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//select[#class='ng-valid ng-touched ng-dirty']//option[#value='1: Agent KPI']"))
).click()
The other approach tried is:
driver.find_element(By.XPATH, "//select[#class='ng-valid ng-touched ng-dirty']//option[#value='1: Agent KPI']")
All these options are resulting in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[#class='ng-valid ng-touched ng-dirty']"}
But this didn't work either. I used implicit wait as well but I am getting the same error. I checked the HTML and there's no iframe within the same. I have been using selectorhub to retrieve XPATHs of elements so pretty sure they're correct. I also used CSS selectors and tag names but still stuck on the same.
I looked everywhere but despite the various threads and answers, I'm still stuck on this issue. Could anyone advise me on what's going wrong here?
Thanks!
It seems you are nearly there. If I take the reference of your page element I can see the class name you have provided seems wrong.
Try this below xpath.
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//select[#class='ng-pristine ng-valid ng-touched']//option[#value='1: Agent KPI']"))
).click()
Related
I am trying to web scraping a popular movie database on the Internet. After a few thousands of requests, my spider is detected and I have to manually click on a simple button to continue my data collection.
I have tried to automatize this process by clicking automatically in that button. For that purpose, I am using Selenium library. I have located the xpath of the button, but, for some reason, .click() method doesn't work.
Here is my code:
def click_button():
options = Options()
options.headless = True
options.add_argument("--window-size=1920,1200")
DRIVER_PATH = 'geckodriver'
driver = webdriver.Firefox(options=options, executable_path=DRIVER_PATH)
driver.get('https://www.filmaffinity.com/es/main.html')
element = driver.find_element(By.XPATH,"//input[#value='Send']")
element.click()
driver.quit()
Also, I have tried other common alternatives, such as waiting until the element gets clickable or visible, but this didn't work. I have also tried to execute the click as a javascript script.
This is the window I see when my spider is detected:
Too many request window (picture)
As you see, there is a reCaptcha-protection icon on the lower right corner, but I don't have to solve any captcha puzzle to confirm I am not a robot, I have just to click on the send button you can see on the picture.
The html which contains the button is the following one:
<div class="content">
<h1>Too many requests</h1>
<div class="image">
<img height="400" src="/images/too-many-request.jpg">
</div>
<div class="text">
Are you sure you do not dream of electric sheep?
</div>
<form name="toomanyrequest">
<div class="alert">
please enter the Captcha to continue.
</div>
<div>
<input type="submit" value="Send">
</div>
</form>
</div>
Which do you think is the trouble with my code or approach? I have to find a way to click on that button to continue my scraping, but I don't know what I am doing wrong (I have little experience at this field).
Maybe the xpath is not correct? Maybe the captcha protection is blocking the click action? I don't get any exception when I execute my code; but nothing happens and the "Too many request" window does not disappear.
Thank you very much.
Try this:
driver.execute_script("arguments[0].click()", element)
or:
driver.execute_script("arguments[0].click();", element)
I'm a newbie in coding with python and selenium. I try to find an element in a web-page by its class name.
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf"
autocomplete="username" spellcheck="false" tabindex="0"
aria-label="Adresse e-mail ou numéro de téléphone" name="identifier"
value="" autocapitalize="none" id="identifierId" dir="ltr"
data-initial-dir="ltr" data-initial-value="">
But each time I've tried I have this error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".whsOnd zHQkBf"}
I don't understand why because I verified and I think that it's not in an iframe so I don't understand why do I have this message and I don't know how to manage the error.
Here are the pictures of my code and the html :
My code
The HTML of the page
Hard to tell without seeing your code and the html. Are you sure it is not in a iframe?
If not you can perhaps try to use xpath to find the element.
When inspecting the webpage in chrome you can right click the element and say copy -> copy xpath. I do recommend looking into xpath, so you know what you are searching for.
Edit:
Try using only one class name, since find_element_by_class_name() only accepts one.
This:
driver.find_element_by_class_name("zHQkBf")
instead of this:
driver.find_element_by_class_name("whsOnd zHQkBf")
I'm trying to click on an element (radio button) using Selenium (in Python), I can't disclose the URL because it's a private corporate intranet, but will share the relevants part of code.
So basically this element is within an iframe, thus, I've used the following code to get the element:
# Select the item on main DOM that will udpate the iframe contents
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='sm20']"))).click()
# Don't sleep, but only WedDriverWait...
wait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='ifrInterior']")))
# Select the element inside the iframe and click
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo']"))).click()
The HTML code for the element is this:
<span class="W1">
<input type="radio" name="gestionesPropias_Equipo" value="1" onclick="javascript:indicadorPropiasEquipo(); ocultarPaginacion('1'); limpiarDatosCapaResultado();">
</span>
I'm interested in clicking this because when we click on it a drop-down is enabled:
If clicked then the dropdown is enabled:
The intersting HTML code for this is
<span id="filtroAsignado" class="W30">
<select name="nuumaAsignado" class="W84">
<option value="">[Todo mi equipo]</option></select>
</span>
Debugging a bit Selenium I can see that the elemtn is found:
And this is actually the base64 image of the element, which is the expected radio button
So I'm wondering why the element actually does not get clicked??
UPDATE: Based on request from #DebanjanB, I'm adding the HTML code from the iframe, which is enclosed inside a div in the main page:
<div id="contenido">
<iframe frameborder="0" id="ifrInterior" name="ifrInterior" src="Seguimiento.do?metodo=inicio" scrolling="auto" frameborder="0"></iframe>
</div>
Actually if I look for the word "iframe", there's only one...
Now checking the iframe source itself, has several iframes hidden but the element I need to interact with is in the iframe mentioned above, the only thing that I forgot to mention is that it's inside a form, but I guess that's not relevant? You can see the whole structure in the following image:
Great question. If you know that selenium found the element, you can use Javascript to click the element directly.
The syntax is:
driver.execute_script("arguments[0].click();", element)
You can also do this, which works the same way:
automate.execute_script("arguments[0].click();", wait.until(EC.element_to_be_clickable((By.XPATH, 'Your xpath here'))))
Essentially you are having Selenium run a javascript click on the element you have found which bypasses Selenium. Let me know if this helps!
I don't see any such issue with your code block either. Perhaps you can try out either of the following options:
Using ActionChains:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']")))).click().perform()
Using executeScript() method:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']"))))
I have a problem finding an Element (Button to click) with my code. I am using Selenium IDE to find the xpath or css selector of the element. Strangely the Selenium IDE does find it but my code doesn't.
The element i want to select is a button, when clicked a dropdown appears and i need to click on another button.
Button (button class="js-select uk-button uk-button--select">)
Button(li data-field="sapOrderNumber">Auftragsnummer
Here is the html of the class i want to select:
<div class="uk-button-dropdown uk-margin-right" data-uk-dropdown="{mode:'click'}" aria-haspopup="true" aria-expanded="false">
<button class="js-select uk-button uk-button--select">
<span>Filter auswählen</span>
<i class="icon-arrow-left"></i>
</button>
<div class="uk-dropdown uk-dropdown-close uk-dropdown-bottom uk-dropdown--select" aria-hidden="true">
<ul class="js-filter-select uk-nav uk-nav--select">
<li data-field="articleNumber">Artikelnummer</li>
<li data-field="dateRange">Zeitraum</li>
<li data-field="sapOrderNumber">Auftragsnummer</li>
<li data-field="personalReference">persönliche Referenz</li>
<li data-field="shipTo">Lieferadresse</li>
<li data-field="orderType">Auftragsart</li>
</ul>
</div>
</div>
Screenshot of Selenium IDE (Selenium IDE does find and click on the button) 1
I have tried to find the element with the following code:
driver.find_element_by_xpath("//ul[#id='tabs-orderbook']/li/div/div/div/div/div/button").click()
driver.find_element_by_xpath("//button").click()
driver.find_element_by_css_selector(.uk-margin-right > .js-select").click()
this is the error message i get:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//ul[#id='tabs-orderbook']/li/div/div/div/div/div/button"}
I have looked for an answer quite a while now and getting frustrated. I would be really grateful for any help or tips.
I am using Chrome ( Version 77.0.3865.120 ), chrome webdriver (Version 77.0.3865.40), selenium (Version 3.141.0)
ps. This is my first question on stack overflow so if you have any tips regarding how i ask questions i would be really grateful as well.
Thanks a lot and have a great day. :D
As per my understanding you want to select the buton with class name "js-select uk-button uk-button--select"..below is the xpath for that,
//button[contains(#class, 'js-select uk-button uk-button--select')]
For selecting the drop down value below is the xpath,
//li[contains(text(), 'Auftragsnummer')]
I have a HTML page containing a form with an tag. I want to set the value of the drop down in this tag using Selenium.
This is how I retrieve the input element:
driver.find_element_by_xpath("/html/body/div[2]/div/div/form/div/div[1]/div[3]/div[1]/div/div[1]/input")
I tried to set the value using select_month.send_keys("09") but this is not accepted by the web page when I try to submit the form so I need to find another method.
EDIT: Here is the HTML of the form, I have ensured that it is the right element in my x-path:
<input autocomplete="off" tabindex="-1" class="ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched" ng-click="$select.toggle($event)" placeholder="Select month" ng-model="$select.search" ng-hide="!$select.searchEnabled || ($select.selected && !$select.open)" ng-disabled="$select.disabled" type="text">
After messing around a bit and incorporating the better practice presented by alecxe, this solution worked...
driver.find_element_by_xpath("//input[#placeholder='Select month']").click()
driver.find_element_by_xpath("//*[contains(text(), '09')]").click()