Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我需要用Selenium单击javascrip按钮_Python 3.x_Selenium - Fatal编程技术网

Python 3.x 我需要用Selenium单击javascrip按钮

Python 3.x 我需要用Selenium单击javascrip按钮,python-3.x,selenium,Python 3.x,Selenium,我是Python新手,我需要点击javascript按钮,但我不知道怎么做。我写道: 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.C

我是Python新手,我需要点击javascript按钮,但我不知道怎么做。我写道:

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')
html代码是:

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


我需要知道如何单击该表单并在下一页(通过按钮打开的页面)中读取数据。您能提出任何解决方案吗?

如果您的代码一切正常,您需要在单击selenium中的javascript按钮以解析新页面后选择新url

如果javascript按钮创建新选项卡,您可以使用

browser.window_handles
currentURL = browser.getCurrentUrl()
BeautifulSoup(currentURL,"lxml")
或新窗口一些复杂的有用链接:

单击带有Java脚本的按钮:

search= driver.find_element_by_xpath("//input[@title='Searching']")
driver.execute_script("arguments[0].click();", search)
假设新页面在同一个选项卡中打开(您并没有另外指出),您可以使用下面的代码来读取数据。同样,您应该指定要读取的数据。因为阅读特定的元素总是一个更好的主意,而不是从整个页面中寻找数据

time.sleep(7)
pageData = driver.page_source
soup = BeautifulSoup(pageData , 'html.parser')

那么你的确切问题是什么?你不能点击按钮吗?或者从打开的页面读取数据?或者两者兼有。谢谢法提赫。当我解决这个问题时,我尝试用你的方法解析de new pageAttributeError:“WebDriver”对象没有python 3的属性“getCurrentUrl”,正确的表达式是>>>currentURL=browser。current\u url这是我的错是正确的代码是browser.current\u url。谢谢rahul rai这是解决方案。很高兴提供帮助。如果这有助于解决您的问题,请向上投票。干杯:)