1

I'm novice in Python and I need to do click on a javascript button, but I don't know how to do it. I wrote this:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from bs4 import BeautifulSoup

chrome_path= r'C:\Users\fritz\Desktop\python\chromedriver.exe'
browser= webdriver.Chrome(chrome_path)
browser.get('url')

lista=[1503,5789]

for i in lista:
    item=browser.find_element_by_name('book')
    item=str(i) #write element in the textbox
    data=browser.execute_script('Searching') #the javascript button
    data=data.click() 
    soup= BeautifulSoup(data,'html.parser')

The html code is:

<td width="28%" align="right">
    <input type="image" src="/web/photo/search_fo.png" alt="Searching" 
        title="Searching">
</td>

I need to know how I can click on that form and read the data in the next page (opened page by the button). Can you suggest any solution, please?

aKratos
  • 191
  • 10
  • So what is your exact issue ? Are you not able to click the Button ? or readind data from opened page? or both. – rahul rai Aug 26 '20 at 04:16

2 Answers2

1

To Click the button with Java Script:

search= driver.find_element_by_xpath("//input[@title='Searching']")
driver.execute_script("arguments[0].click();", search)

Assuming new page is opening in same tab (As you have not indicated otherwise.) You can use below code to read data. Again you should have specified what data you want to read. As its always a better idea to read particular element rather than scrape complete page and find data from that.

time.sleep(7)
pageData = driver.page_source
soup = BeautifulSoup(pageData , 'html.parser')
rahul rai
  • 2,260
  • 1
  • 7
  • 17
0

if everything okey in your code, you need to select new url after click javascript button in selenium for parse new page

if javascript button create new tab ,you may use

browser.window_handles
currentURL = browser.getCurrentUrl()
BeautifulSoup(currentURL,"lxml")

or new window some complicated useful link: How to switch to new window in Selenium for Python?

Fatih Ekici
  • 123
  • 8