Python mechanize doesn't click a button - python

check the following script:
from mechanize import Browser
br = Browser()
page = br.open('http://scottishladiespool.com/register.php')
br.select_form(nr = 5)
r = br.click(type = "submit", nr = 0)
print r.data
#prints username=&password1=&password2=&email=&user_hide_email=1&captcha_code=&user_msn=&user_yahoo=&user_web=&user_location=&user_month=&user_day=&user_year=&user_sig=
that is, it doesn't add the name=value pair of the submit button (register=Register). Why is this happening? ClientForm is working properly on other pages, but on this one it is not. I've tried setting the disabled and readonly attributes of submit control to True, but it didn't solve the problem.

There is a disabled=disabled attribute on the register button. This prevents the user from clicking and presumably mechanize respects the disabled attribute as well.
You'll need to change the source code of that button. Enabling the control means completely removing the disabled=disabled text.

Related

Why is selenium not able to find element with ID, even when it is not in an iframe?

I am trying to make an automatic program for logging in to GitHub. I could find only the sign-in option. After that, I could not find the Username field. I have confirmed that the element is definitely not in a/an (i)frame. I have tried an alternative with css-selector.
Here is the code I tried:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
chrome_opt = Options()
chrome_opt.add_experimental_option("detach", True) # type: ignore[unknown]
auto = Chrome(options=chrome_opt)
auto.get("https://github.com")
signin_link = auto.find_element("link text", "Sign in")
signin_link.click()
username = auto.find_element("id", "login_field")
username.send_keys("ArnabRollin") # type: ignore[unknown]
# FIXME
The type-ignore comments are there because of 'strict mode' type checking in VS Code. Also, after 5 tries of running it, it finally worked, but when I ran it again it didn't.
now your code is looking for elements in the page https://github.com - the one used in method get()
instead of clicking element's link, get it with webdriver:
signin_link = auto.find_element("link text", "Sign in")
signin_link.click()
use
signin_link = auto.find_element("link text", "Sign in").get_attribute('href')
auto.get(signin_link)
auto.get(url2) will save new page context into driver. after sign in is complete, a new page context will be needed
Note, I'm not sure it's ethical scraping this website, and besides, they have Captcha.
You can use this CSS selector:
username = auto.find_element(By.CSS_SELECTOR, "input.js-login-field")
Additionally, when you go to github.com and click on login, the URL changes to /login: https://github.com/login

Why Selenium method get(), doesnt work second time?

Help me figure out why when using the get () method a second time, the page does not go? The method works only if you use a time delay time.sleep ()
Not working:
LOGIN = 'something#mail.com'
PASS = 'somepass'
LINK = 'https://stepik.org/'
browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')
Working
LOGIN = 'something#mail.com'
PASS = 'somepass'
LINK = 'https://stepik.org/'
browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
time.sleep(5)
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')
You are trying to login into the web site and then to navigate to some internal page.
By clicking the submit button
button = browser.find_element_by_xpath('//button[#type = "submit"]').click()
You are trying to log into the site.
This process takes some time.
So if immediately after clicking the submit page, while login is still not proceed, you are trying to navigate to some internal page this will not work since you still not logged in.
However you do not need to use a hardcoded sleep of 5 seconds.
You can use an explicit wait of expected conditions like presence_of_element_located() of some internal element to indicate you are inside the web site. Once this condition is fulfilled you can navigate to the desired internal page.
Try an alternative way:
driver.navigate().to("https://stepik.org/lesson/237240/step/3?unit=209628")

Python code to fill and submit Stulish Form

I'm trying to write a Python code to submit a simple form.
http://stulish.com/soumalya01
[Edit : http://travelangkawi.com/soumalya01/
When you use this link it returns a different page on form submit. Is good for debugging]
Any code would do
Tried both mechanize and mechanical soup. Both are unable to handle the text fields. It does not have a name only ID. But we are unable to get the element by ID
Any Code would do as long as it works. (Fill ABC in the text box and hit submit)
I just followed the documentations of mechanize. See sample code below:
from mechanize import Browser
br = Browser()
br.open('http://stulish.com/soumalya01')
br.select_form(nr=0)
form.set_all_readonly(False) #add this
br.form.set_value('ABC', nr=1)
print(br.form.controls[1])
br.submit()

Python selenium select modal

I am trying to get the google map embed url using selenium. I am able to click the share button and the page shows a modal with share url and an embed url. However i am un able to switch the dialog box.
Here is my code
browser.get('https://www.google.com/maps/place/%s?hl=en'%(code))
time.sleep(3)
share_class = "ripple-container"
buttons = browser.find_elements_by_class_name(share_class)
for but in buttons:
x = but.text
if x == 'SHARE':
but.click()
modal = browser.switch_to.active_element
share = modal.find_element_by_id("modal-dialog")
print(share.text)
here is the image.
You don't need to switch to the modal dialog, you can access it just like you would any other HTML on the page. You can simplify your code to
browser.get('https://www.google.com/maps/place/%s?hl=en'%(code))
browser.find_element_by_xpath("//button/div[.='SHARE']").click()
url = browser.find_element_by_id("last-focusable-in-modal").text
print(url)
But... if you read the dialog, you will see that it states
You can also copy the link from your browser's address bar.
so the URL you are navigating to in the first line is what you are going to copy from the Share link so there's really no point. You already have the URL.

Python Mechanize click button without a form

I use the Mechanize for filling the form of filtering.
My code:
br = Browser()
br.open(self.domain)
br.select_form(nr=1)
br.find_control("pf_keywords").value = "Lisp"
response = br.click(type='button', nr=0)
#or
response = br.submit(label='Применить фильтр')
At the same time, submit button is not in the list of controls for this form.
Html code for this button:
<button type="button" class="b-button b-button_flat b-button_flat_green" onclick="$('frm').submit();">Применить фильтр</button>
Because of this, using the method click() and submit() impossible to produce form submission. In these methods, there is a search of the desired control with the parameters passed to the control environment forms, as desired button is not there, raise a bug :
mechanize._form.ControlNotFoundError: no control matching type 'button', kind 'clickable'
What should I do? How can push a button and get the result?

Categories

Resources