Unable to locate send button in whatsapp api - python

I am trying to send a message through web whatsapp using python selenium.
here is my code.
from selenium import webdriver
import time
browser=webdriver.Chrome()
browser.get("""https://api.whatsapp.com/send?phone=************&text=I'm%20interested%20in%20your%20car%20for%20sale""")
time.sleep(5)
send_btn=browser.find_element_by_id("action-button")
send_btn.click()
It is not clicking the send button, it just blinks. please help.

As you have mentioned, you are using XPATH , I would suggest you to use CSS_SELECTOR over XPATH.
This is the code you can try out :
send_button = driver.find_element_by_css_selector('a.button.button--simple.button--primary')
send_button.click()
UPDATE :
CSS selectors perform far better than Xpath and it is well documented in Selenium community. Here are some reasons,
Xpath engines are different in each browser, hence make them inconsistent.
IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.
For more you refer this SO Link : XPATH VS CSS_SELECTOR

Related

python selenium Unable to locate element for Google map term and service

I am trying to automate the search for the short google link via this code:
link = 'https://www.google.com/maps/place/Sport+La+Pava/#41.273359299999996,2.0005245,14z/data=!4m8!1m2!2m1!1sSport+La+Pava!3m4!1s0x12a49d5b3d4b1753:0xeb7e41655fa9ec91!8m2!3d41.273359299999996!4d2.0005245'
from selenium import webdriver
CHROME_DRIVER_PATH = "D:\chromedriver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
driver.get(link)
time.sleep(3)
button1 = driver.find_element_by_id("introAgreeButton")
button1.click()
new_https = driver.find_element_by_xpath('/html/body/jsl/div[3]/div[2]/div/div[2]/div/div[3]/div/div/div[1]/div[4]/div[2]/div[1]/input').value_of_css_property()
print(new_https)
the link is a google map link.
The error happen at button1 = driver.find_element_by_id("introAgreeButton"). The button I am trying to get through here is basically the term and condition. I have to accept it. but everytime I receive the error NoSuchElementException
I have tried different method: using Xpath, full Xpath, css, nothing work.
I use the same code for website like amazon.com all work fine there, so it is not about the location of my webdriver or anything like that. it seems quite Google term and condition specific
As #Nick pointed out, this question has been answered (code was in javascript) Here is the code in Python for thoe who need it:
driver.switch_to.frame(0)
driver.find_element_by_id("introAgreeButton").click()

How to click this button with python & selenium

I'm looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.
I'm having trouble identifying the button to then call the click function.
Here's the HTML
Alternatively perhaps I could run the 'ng-click' function, unsure how to approach that. If I do this, I'll need to pipe through 'index', from what I can tell from this HTML (my understanding of HTML is minimal)
Thank you
You can have browser to figure out the button CSS selector for you.
Here's how to do that in Chrome:
Open your page in Chrome
Right-click on your button and select the Inspect Element from the context menu
The Inspector window will open with the button's HTML markup selected.
Right-click the selected HTML line(s) and select Copy -> CSS Selector.
Paste the CSS selector into the code below
And here's the code to click your button:
from selenium import webdriver
browser = webdriver.Chrome('/path/to/chromedriver')
browser.get('your/website/url')
button = browser.find_element_by_css_selector('paste the CSS selector here')
button.click()
Hope this helps.
PS: Here's an excellent article (a chapter from the Automate the boring stuff with Python book) on web scraping and browser automation using BeautifulSoap and Selenuim.
Try for xpath as:
//div[#id='channel']//div[#class='channel-list']/div/div/div[#class='ch-btn play']
or
//div[#id='channel']//div[#class='channel-list']//div[#class='ch-btn play']
Let me know if this Answers your Question.
You'll probably want to use CSS Selectors, as they are preferred in selenium over Xpath. Some important notes about html and selenium:
Html is a static language. There is not way to "call" or "run" things in it. That requires use of a different language, like JavaScript.
Selenium mimics an actual user, so selenium is not directly "calling" anything, it is interacting with the page, and the page responds.
python code:
driver = webdriver.Chrome('path/to/chromedriver')
driver.get('your/site/here')
# This is a css selector for the div that you want to click on.
css_selector = "div[ng-click='play($index)']"
# This finds the object that is located at css_selector
button_element = driver.find_element_by_css_selector(css_selector)
# Sends a mouse click to the button_element
button_element.click()

Python 2.7 - Detect opened browser popup window [duplicate]

Is there a possibility if I code a program in python that allows to automatically browse a given website using mechanize to detect if there are popup windows (suggesting advertisements or downloading actions ...) using Python ?. I would appreciate any hint (for example, if you give me a library that fulfills this task I would be very happy)
Mechanize cannot handle javascript and popup windows:
How do I use Mechanize to process JavaScript?
Mechanize and Javascript
To accomplish the goal, you need to utilize a real browser, headless or not. This is where selenium would help. It has a built-in support for popup dialogs:
Selenium WebDriver has built-in support for handling popup dialog
boxes. After you’ve triggerd and action that would open a popup, you
can access the alert with the following:
alert = driver.switch_to_alert()
Example (using this jsfiddle):
from selenium import webdriver
url = "http://fiddle.jshell.net/ebkXh/show/"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_xpath('//button[#type="submit"]')
# dismiss
button.click()
driver.switch_to.alert.dismiss()
# accept
button.click()
driver.switch_to.alert.accept()
See also:
Handle Popup Windows
Click the javascript popup through webdriver
Unfortunately, Mechanize's browser seems to skip the pop-ups so the title, URL, and HTML are identical for both pop-ups and normal pages.
Frankly, Python is not the right tool for this job and is lagging behind in this respect IMHO. Having spent months doing web crawling, for sites that use Javascript extensively (the number of which is greatly increasing nowadays), I find that using Javascript-Based environments like PhantomJS or SlimerJS are simply better for what you're trying to do.
If you have the luxury to use Javascript-Based environments, I'd say go right ahead. However, you can still use python. PhantomJS embeds Ghost Driver. You can use Ghost.py to utilize the power of PhantomJS. Or you can use Selenium with Python as illustrated here.

Selenium - 'site' object has no attribute 'find_element_by_link_text'

I'm trying to write a python script that clicks a certain link in a table on a webpage. The only option I have to select this particular link is it's link text, but selenium keeps telling me that the command "find_element_by_link_text" doesn't exist even though it's found on not only the official selenium docs but also multiple online selenium examples. Here's the code snippet:
hac.find_element_by_link_text("View this year's Report Cards").click()
I cross-checked my selenium installation with one from the website and they seem to be the same. Was this feature deprecated or am I just missing something? I'm using selenium v.2.45.0 and python v.2.7.
You need to call the find_element_by_link_text() method using driver.
Here is a sample script that opens the Python home page, locates the link to the About page using its link text, and then clicks that link:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.implicitly_wait(10)
elem = driver.find_element_by_link_text("About")
driver.implicitly_wait(10)
elem.click()
This page of the Selenium docs gives an overview of all of the find_element methods available, and shows how to call those methods.
If you are using python selenium 4.3 some methods are deprecated like
find_element_*() and find_elements_*()
Examples:
find_element("name","element_name")
find_element("xpath","xpath_here")

How to check for webpages' popups?

Is there a possibility if I code a program in python that allows to automatically browse a given website using mechanize to detect if there are popup windows (suggesting advertisements or downloading actions ...) using Python ?. I would appreciate any hint (for example, if you give me a library that fulfills this task I would be very happy)
Mechanize cannot handle javascript and popup windows:
How do I use Mechanize to process JavaScript?
Mechanize and Javascript
To accomplish the goal, you need to utilize a real browser, headless or not. This is where selenium would help. It has a built-in support for popup dialogs:
Selenium WebDriver has built-in support for handling popup dialog
boxes. After you’ve triggerd and action that would open a popup, you
can access the alert with the following:
alert = driver.switch_to_alert()
Example (using this jsfiddle):
from selenium import webdriver
url = "http://fiddle.jshell.net/ebkXh/show/"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_xpath('//button[#type="submit"]')
# dismiss
button.click()
driver.switch_to.alert.dismiss()
# accept
button.click()
driver.switch_to.alert.accept()
See also:
Handle Popup Windows
Click the javascript popup through webdriver
Unfortunately, Mechanize's browser seems to skip the pop-ups so the title, URL, and HTML are identical for both pop-ups and normal pages.
Frankly, Python is not the right tool for this job and is lagging behind in this respect IMHO. Having spent months doing web crawling, for sites that use Javascript extensively (the number of which is greatly increasing nowadays), I find that using Javascript-Based environments like PhantomJS or SlimerJS are simply better for what you're trying to do.
If you have the luxury to use Javascript-Based environments, I'd say go right ahead. However, you can still use python. PhantomJS embeds Ghost Driver. You can use Ghost.py to utilize the power of PhantomJS. Or you can use Selenium with Python as illustrated here.

Categories