How can I click second button on one webpage using selenium python3? - python-3.x

Working in python, selenium, Win 7, I want to click on sowcomment button after it is able to clickable that is located on this webPage, handled by wait then I want to click on showmore Comments button to see more comments in order to scrape more comments. After first button, i am able to extract comments.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import selenium.common.exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import selenium.webdriver.support.ui as UI
driver = webdriver.Firefox()
driver.get("http://www.aljazeera.com/news/2016/07/erdogan-west-mind-business-160729205425215.html")
wait = UI.WebDriverWait(driver, 10)
next_page_link = wait.until(
EC.element_to_be_clickable((By.ID, 'showcomment')))
next_page_link.click()
wait = UI.WebDriverWait(driver, 20)
next_page_link2 = wait.until(
EC.element_to_be_clickable((By.LINK_TEXT, 'Show more comments')))
next_page_link2.click()
v = driver.find_elements_by_class_name('gig-comment-body')
print(v)
for h in v:
print(h.text)
but second button is unable to click rather giving the exception:
selenium.common.exceptions.TimeoutException:
What is the problem?

I think you should try using execute_script() to perform click as below :
next_page_link2 = wait.until(
EC.element_to_be_clickable((By.XPATH, '//div[contains(text(),"Show more comments")]')))
#now click this using execute_script
driver.execute_script("arguments[0].click()", next_page_link2)
Hope it helps...:)

Related

Not being able to click on a button using Python selenium with error ElementNotInteractableException

I am using Python Selenium to open https://www.walmart.com/ and then want to click "Create an account" tab under "Sign In Account" as shown in this Walmart website pic. The source code of button from Walmart website along with the image is as follows:
<a link-identifier="Create an account" class="no-underline" href="/account/signup?vid=oaoh">
<button class="w_C8 w_DB w_DE db mb3 w-100" type="button" tabindex="-1">Create an account</button>
</a>
Walmart Website Source code-Pic
My python code for opening https://www.walmart.com/ for accessing Create an account button and click on it is as follows:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
url = "https://www.walmart.com/"
s=Service('C:/Users/Samiullah/.wdm/drivers/chromedriver/win32/96.0.4664.45/chromedriver.exe')
driver = webdriver.Chrome(service=s)
driver.get(url)
driver.maximize_window()
elems = driver.find_element(By.XPATH, '//button[normalize-space()="Create an account"]')
time.sleep(3)
elems.click()
However I am getting this error of ElementNotInteractableException as shown by this Error Pic.
Can anyone guide me what is wrong with my code/approach?.
Thanks in advance
You can not click this element directly, you have to hover over "Sign In" element to make this element appeared.
It is also highly recommended to use expected conditions.
This should work:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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.action_chains import ActionChains
url = "https://www.walmart.com/"
s=Service('C:/Users/Samiullah/.wdm/drivers/chromedriver/win32/96.0.4664.45/chromedriver.exe')
driver = webdriver.Chrome(service=s)
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get(url)
driver.maximize_window()
sign_in_btn = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Sign In']")))
actions.move_to_element(sign_in_btn).perform()
time.sleep(0.5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[normalize-space()="Create an account"]'))).click()

Not able to click on Check box using Selenium Python

I want to click on the check box, but my code is not working. Steps which I want to follow is
Open website
Select Profession e.g:- Dental
License Type e.g :- Dentist
Enter a alphabet with * to get all the records
Click on the check box
Click on search button
Script also ask for image verification for which I am unable to process, any help would be appriciated
home_page = 'https://forms.nh.gov/licenseverification/'
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import csv
from shutil import copyfile
import datetime
import subprocess
import time
import multiprocessing
import sys
import subprocess
current_date=datetime.date.today()
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(r"C:\Users\psingh\AppData\Local\Programs\Python\Python38-32\chromedriver.exe")
driver.get(home_page)
select_prof = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "t_web_lookup__profession_name")))
Select(select_prof).select_by_value('Dental')
select_lic_type = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "t_web_lookup__license_type_name")))
# select profession criteria value
Select(select_lic_type).select_by_value('Dentist')
time.sleep(1)
send = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "t_web_lookup__last_name"))
)
send.send_keys('A*')
time.sleep(1)
# click on check box
driver.find_element_by_xpath("/html/body/div[1]").click()
# click search button
driver.find_element_by_id("sch_button").click()
# wait to get the result
time.sleep(1)
The check box which you are trying to click is inside the iframe. First switch to that iframe and then try to click on that checkbox.
Below code works fine in pycharm and windows 10.
driver = webdriver.Chrome(PATH)
url = 'https://forms.nh.gov/licenseverification/'
driver.get(url)
driver.maximize_window()
sleep(5)
select_prof = driver.find_element(By.ID, 't_web_lookup__profession_name')
Select(select_prof).select_by_value('Dental')
select_lic_type = driver.find_element(By.ID, "t_web_lookup__license_type_name")
# select profession criteria value
Select(select_lic_type).select_by_value('Dentist')
sleep(1)
send = driver.find_element(By.ID, "t_web_lookup__last_name")
send.send_keys('A*')
sleep(1)
# click on check box
driver.switch_to.frame(0)
captcha = driver.find_element(By.CSS_SELECTOR, '.recaptcha-checkbox-border')
captcha.click()
# click search button
driver.find_element(By.XPATH, "sch_button").click()
sleep(1)

Scroll to right in Tradingview chart using python

I am trying to scroll right using selenium python in TradingView
My Code:
driver.find_element_by_xpath("//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']").click()
It throws an error:
selenium.common.exceptions.ElementNotInteractableException: Message:
element not interactable
Try the below code -
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 60)
action = ActionChains(driver)
driver.maximize_window()
driver.get('https://www.tradingview.com/chart/')
try:
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class,\"closeIcon\")]"))).click()
except:
pass
RightScroll = wait.until(EC.presence_of_element_located((By.XPATH,"//div[contains(#class,\"btn--move-right\")]")))
for i in range(10):
time.sleep(2)
print(i)
action.move_to_element(RightScroll).click().perform()
If it solves your problem then please mark it as answer. Do let me know if you have any query.
There is a message window displayed when you first load the page. You have to close it first (to get the right arrow container visible) if you are not doing it by
driver.findElement(By.xpath(".//span[contains(#class,'closeIcon')]")).click();
driver.findElement(By.xpath(".//td[#class='chart-markup-table time-axis']")).click();//To get the right arrow container visible
driver.findElement(By.xpath(".//div[#class='control-bar__btn control-bar__btn--move-right apply-common-tooltip']")).click();

How to click element using selenium web driver(Python)

I am trying to click an element using selenium chromedriver by the Use of ID of an element to click.
I want to Click Year '2020" from the following webpage: 'https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan'
I tried with the below code.
driver = webdriver.Chrome(executable_path=ChromeDriver_Path, options = options)
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
Id = "ctl00_ContentPlaceHolder1_gvMajorIncident_ct123_lbtnYear" ### Id of an Element 2020
wait = WebDriverWait(driver, 20) ##Wait for 20 seconds
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
time.sleep(10)
but unfortunately this gives an Error as below:
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
File "C:\Users\Pavan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Please anyone help me on this... Thanks;
I don't know if your imports were correct. Also, your code doesn't scroll to the bottom of the page. Use this.
import os
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path='driver path')
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
driver.maximize_window()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="ctl00_ContentPlaceHolder1_gvMajorIncident_ctl23_lbtnYear"]')))
element.click()

Scrape Table With JavaScript "Show More Button" In Webpage

I am attempting to pull a data table from a NYT open-access web article on the number of COVID-19 cases, which can be found here. The table shows the top 10 states with highest number of cases, and expands to all 50 states and U.S. territories upon clicking the "Show more" button.
The HTML portion of the table is as follows:
Using this tutorial, I have written the following code utilizing Selenium to try clicking this button, and pass this page off to BeautifulSoup to begin synthesizing for use in Pandas. My initial code looks as follows:
from bs4 import BeautifulSoup
import selenium
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome("/usr/bin/chromedriver", chrome_options=options)
driver.get("https://www.nytimes.com/interactive/2020/us/coronavirus-us-cases.html")
At this juncture, I am not sure how to execute clicking the button (found in the HTML snippet: <button class="svelte-1tjczrs">Show more</button>), and stage it for BeautifulSoup.
Any help is greatly appreciated!
Try the following attempt. It should fetch you the required content unveiling show more button and put the same in a dataframe.
import pandas
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
link = "https://www.nytimes.com/interactive/2020/us/coronavirus-us-cases.html"
with webdriver.Chrome() as driver:
driver.get(link)
datalist = []
show_more = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"#g-cases-by-state button[class^='svelte']")))
driver.execute_script("arguments[0].click();",show_more)
for elem in WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"#g-cases-by-state table[class^='svelte'] tr"))):
data = [item.text for item in elem.find_elements_by_css_selector("th,td")]
datalist.append(data)
df = pandas.DataFrame(datalist)
print(df)

Resources