This is the html code:
<div id="dcf-page-export-buttons" class="no-print" style="display: block;">
<a id="dcf-saveaspdf" href="#" target="_blank" class="k-button">
Save as PDF
</a>
<a id="dcf-saveaspng" href="#" target="_blank" class="k-button">
Save as Image
</a>
<a id="dcf-printPdf" class="k-button" href="#">
Print
</a>
<a id="dcf-btnClose" class="k-button" href="#">
Close
</a>
</div>
I want to click on the Print href but it isn't being clicked. Here is my code:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
print = driver.find_element_by_id("dcf-printPdf")
print.click()
Before finding element by id for print, I had clicked on Export href which opened a new tab and after opening of the new tab, I'm trying to click on print but getting an error. This is the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Let me know if I'm going wrong somewhere or if there is a problem in the html.
The first part of the question has been answered. This is the 2nd part:
On clicking on this Print button:
This window gets opened. It's not a new tab per se, but just a new window. Within that window, I want to click on the Save button. Is there a way to go about doing that? This is how the view looks like:
And here is the html code.
<cr-button class="action-button" aria-disabled="false" role="button" tabindex="0">
Save
</cr-button>
Here is my code till now:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
This is the snip of the error log. I've added the snip because I'm unsure of the error.
Small continuation of the error:
You need to switch to new tab first then you can click on Print using that Id that you have been trying.
Switch to new windows like this :
driver.switch_to.window(driver.window_handles[1])
As you can see from the error, the element you trying to access is not interactable.
So, the problem is not with HTML.
I can't see the page you working on, but the problem can be that element you trying to access is out of the view.
Try this
from selenium.webdriver.common.action_chains import ActionChains
print = driver.find_element_by_id("dcf-printPdf")
ActionChains(driver).move_to_element(print).click(button).perform()
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've been trying to build a bot that scrapes the name of followers liking a picture in instagram. The website opens a popup box with the accounts in side, and the box seemingly refreshes the account links as you scroll it. I've written a code that will open the box and then scroll it, but i can't get selenium to scrape the account names. My code looks like this to scroll the pop up box:
realscroll_box = browser.find_element_by_xpath('/html/body/div[4]/div/div[3]/div')
while last_ht != ht:
last_ht = ht
time.sleep(2)
ht = browser.execute_script('''
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
''', realscroll_box)
namelinkstemp1 = realscroll_box.find_elements_by_xpath('//*[contains(#href,"/")')
But it gives me this error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //[starrts-with(#href,"/") because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[starrts-with(#href,"/")' is not a valid XPath expression.
The text i'm trying to extract looks like this:
<a title="instagramusername" href="/instagramusername/">
<div class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm ">
<div class="_7UhW9 xLCgt qyrsm KV-D4 fDxYl rWtOq">
<div class=" Igw0E rBNOH eGOV_ ybXk5 _4EzTm ">instagramusername</div>
</div>
</div>
</a>
Any help is appreciated, xpath queries are very new to me.
Thanks :)
Starts-with function finds the element based on attribute value. In your code function is not correct . Also //*[contains(#href,"/") , here you missed ]
Correct one : //a[contains(#href, '/')]
//*[starts-with(#href,'/')]
or
//a[starts-with(#href,'/instagramuse')]
I have the following HTML
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
and the following Python
btn = driver.find_element_by_name('_eventId_confirmed')
Running this code returns an error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="_eventId_confirmed"]
Just before this non-working HTML/code-combination I have the following HTML element:
<input name="registration" id="registration" class="size-28" maxlength="7" autofocus="" autocomplete="off" type="text" data-original-title="" title="" style="">
which I successfully access with
elem = driver.find_element_by_name("registration")
Why does the second one work but not the first one?
Edit: the problem was that a new window was opened and I needed to switch window handler. So, false alarm. Thank you all.
A "tag name" is not the same as a "name". The former refers to the HTML element's tag name, while the later refers to the HTML element's "name" attribute.
For example, in your first HTML snippet,
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
button is the tag name while _eventId_confirmed is the (attribute) name.
This could be because of the modal dialogue, as you mentioned in a comment. Try
driver.switchTo().frame("ModalFrameTitle");
or
driver.switchTo().activeElement()
You can do it by using window_handles and switch_to_window method.
Before clicking the button the window handle as
window_before = driver.window_handles[0]
elem = driver.find_element_by_name("registration")
after clicking the button the window handle of newly opened window as
window_after = driver.window_handles[1]
then execute the switch to window methow to move to newly opened window
driver.switch_to.window(window_after)
driver.find_element_by_name("_eventId_confirmed").click()
Hope this help.
<a class="btn btn-xs btn-success" href="/user-location-auth/location?id=4013"
title="View User Location Access" data-toggle="tooltip" data-method="post">
<span class="glyphicon glyphicon-map-marker"></span></a> <span
class="glyphicon glyphicon-map-marker"></span> <<pseudo:before>>
</<pseudo:before>>
Hi all, I have been tasked with a project that requires clicking on a button in a table (with 14 cells). The first 13 cells are all unhidden, but the 14th one (where the buttons are located), are hidden unfortunately. I have tried action chains, I have tried hovering over the element and then clicking on it. I seem to be at a loss here. This piece of code did work, however I want to avoid using javascript as I want to replicate a user experience.
z4 = driver.find_element_by_css_selector('a.btn-success > span.glyphicon-map-marker')
driver.execute_script('arguments[0].click();', z4)
Hi everyone and cruisepandey, I found a solution that works. First, I waited for the presence of the element to be located. Once that occurred, I grabbed the element and moved to it with ActionChains (I think I was misusing ActionChains earlier). Then I was able to click on the Bootstrap button.
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, """
//tr[#data-key='2312312321321']/td/a[contains(#class, 'btn-xs btn-
success')]/span[contains(#class, 'glyphicon')]""")))
y = driver.find_element(By.XPATH, """//tr[#data-
key='2312312321321']/td/a[contains(#class, 'btn-xs btn-
success')]/span[contains(#class, 'glyphicon')]""")
#Use action chains to click on the button
ActionChains(driver).move_to_element(y).click().perform()
How to automate webpage button control like having span child by using
driver. findElement_by_xpath ()
on Ubuntu Linux and chromium Autotest
The wesite is: https://www.youtube.com/my_webcam?privacy=public
I would like to click on Start recording button.
HTML source is:
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-dark yt-uix-button-has-icon webcam-record-button" type="button" onclick=";return false;" id="record-button">
<span class="yt-uix-button-icon-wrapper">
<span class="yt-uix-button-icon yt-uix-button-icon-upload yt-sprite">
</span>
</span>
<span class="yt-uix-button-content">Start recording </span>
</button>
We have tried it by using
driver.find_element_by_css_selector()
driver.find_element_by_id()
driver.find_element_by_xpath ()
However, nothing has worked. Could you please suggest us appropriate solution?
The record button is within an iframe, you'll need to switch that iframe first and then locate the button with your chosen selector.
iframe = self.driver.find_element_by_css_selector("#webcam-container")
self.driver.switch_to_frame(iframe)
record = self.driver.find_element_by_css_selector("#record-button > span.yt-uix-button-content")
record.click()