GVKun编程网logo

python selenium单击按钮(python selenium 点击按钮)

10

以上就是给各位分享pythonselenium单击按钮,其中也会对pythonselenium点击按钮进行解释,同时本文还将给你拓展AllegroSelenium查找并单击按钮、ElementNotV

以上就是给各位分享python selenium单击按钮,其中也会对python selenium 点击按钮进行解释,同时本文还将给你拓展Allegro Selenium 查找并单击按钮、ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、Python Selenium - 按钮未点击、Python Selenium 单击按钮,无名称、无 ID、无文本、许多类和 aria 标签等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

python selenium单击按钮(python selenium 点击按钮)

python selenium单击按钮(python selenium 点击按钮)

我是python selenium的新手,我尝试单击具有以下html结构的按钮:

<div>    <divonclick="submitForm(''mTF'')">        <inputtype="button"></input>        <div></div>        <span>           Search        </span>    </div>    <divonclick="submitForm(''rMTF'')">        <inputtype="button"></input>        <span>              Reset        </span>   </div></div>

我希望能够同时单击上方的SearchReset按钮(显然是单独单击)。

我尝试了几件事,例如:

driver.find_element_by_css_selector(''.button .c_button .s_button'').click()

要么,

driver.find_element_by_name(''s_image'').click()

要么,

driver.find_element_by_class_name(''s_image'').click()

但是,我似乎总是以结尾NoSuchElementException,例如:

selenium.common.exceptions.NoSuchElementException: Message: u''Unable to locate element: {"method":"name","selector":"s_image"}'' ;

我想知道是否可以以某种方式使用HTML的onclick属性来进行selenium单击?

任何能将我指向正确方向的想法都很棒。谢谢。

答案1

小编典典

对于python,请使用

from selenium.webdriver import ActionChains

ActionChains(browser).click(element).perform()

Allegro Selenium 查找并单击按钮

Allegro Selenium 查找并单击按钮

第一次加载网站时有一个弹出窗口。你需要关闭这个弹出窗口然后它的工作。

wd = webdriver.Chrome()
wd.get('https://allegro.pl/oferta/zageszczarka-6-5km-90kg-higher-briggs-gratisy-9003885105#aboutSeller')

wd.maximize_window()
wd.find_element_by_xpath("/html/body/div[2]/div[5]/div/div[2]/div[2]/button[1]").click()

click_on_button = wd.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button')
click_on_button.click()
click_on_button = wd.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[2]/ul/li[1]/div/button')
click_on_button.click()
click_on_button = wd.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[7]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[2]/ul/li[2]/div/button')
click_on_button.click()

ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误

ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误

我有一个包含源代码的页面,如下面的代码。在执行操作后,在其中显示“撤消”和“关闭”按钮。我试图单击“关闭”按钮。我已经尝试了下面的所有三个代码块,但都无法正常工作。有人可以指出我做错了什么,还是建议其他尝试?

html来源:

<div><div><i></i><div>Your stuff is going to <span>place</span> is on its way.</div><div><button> Undo</button></div><div><button> Close</button></div></div></div>

代码尝试:

#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()driver.find_element_by_css_selector(''.c-button.c-button--blue'').click()#driver.find_element_by_link_text(''Close'').click()

错误:

---------------------------------------------------------------------------ElementNotVisibleException                Traceback (most recent call last)<ipython-input-15-6d570be770d7> in <module>()      1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()----> 2 driver.find_element_by_css_selector(''.c-button.c-button--blue'').click()      3 #driver.find_element_by_link_text(''Close'').click()~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)     78     def click(self):     79         """Clicks the element."""---> 80         self._execute(Command.CLICK_ELEMENT)     81      82     def submit(self):~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)    626             params = {}    627         params[''id''] = self._id--> 628         return self._parent.execute(command, params)    629     630     def find_element(self, by=By.ID, value=None):~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)    318         response = self.command_executor.execute(driver_command, params)    319         if response:--> 320             self.error_handler.check_response(response)    321             response[''value''] = self._unwrap_value(    322                 response.get(''value'', None))~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)    240                 alert_text = value[''alert''].get(''text'')    241             raise exception_class(message, screen, stacktrace, alert_text)--> 242         raise exception_class(message, screen, stacktrace)    243     244     def _value_or_default(self, obj, key, default):ElementNotVisibleException: Message: element not interactable  (Session info: chrome=72.0.3626.109)  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

答案1

小编典典

与文本的元素 关闭 是一个动态的元素,以便找到你要引起元素 WebDriverWait元素是可点击 ,你既可以使用以下方法解决:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, ''action-bar'')]//button[@c-button c-button--blue'' and normalize-space()=''Close'']"))).click()
  • 注意 :您必须添加以下导入:

        from selenium.webdriver.support.ui import WebDriverWait    from selenium.webdriver.common.by import By    from selenium.webdriver.support import expected_conditions as EC

Python Selenium - 按钮未点击

Python Selenium - 按钮未点击

我看到的问题是您的定位器没有定位页面上的任何元素。您可以通过为 XPath 运行 $x() 和为 CSS 选择器运行 $$() 在 Chrome 开发工具中的页面上测试您的定位器。在您的示例中,您将运行 $x("//button[text() = 'Add to basket']") 并看到它返回 0(空集)。

您可能会遇到的另一个问题是有两个“添加到购物篮”按钮。页面的触摸部分有一个(我假设为移动/平板电脑布局)和您想要的一个。你想要的是 DOM 中的第二个,所以我们需要找到一种方法来创建一个对正确的定位器唯一的定位器。为此,我们可以向定位器添加一个 id = "product-actions" 的 DIV。现在尝试 $$("#product-actions span[data-name='AddToBasket']") 并看到它返回 1。现在我们需要添加一个等待以确保在尝试单击此元素之前页面正确加载。这是更新后的代码:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions() 
options.add_argument("--user-data-dir=C:/Users/pc/AppData/Local/Google/Chrome/User Data/") #Path to your chrome profile
driver = webdriver.Chrome(executable_path="chromedriver.exe",chrome_options=options)
print ("opening webpage")
driver.get(page)

def main():
    wait = WebDriverWait(driver,20)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#product-actions span[data-name='AddToBasket']"))).click()

if __name__ == '__main__':
    main()

Python Selenium 单击按钮,无名称、无 ID、无文本、许多类和 aria 标签

Python Selenium 单击按钮,无名称、无 ID、无文本、许多类和 aria 标签

如何解决Python Selenium 单击按钮,无名称、无 ID、无文本、许多类和 aria 标签?

这是按钮:

<button _ngcontent-qte-c250="" mat-raised-button="" color="primary" aria-label="Close Welcome Banner"><span><mat-icon _ngcontent-qte-c250="" role="img"aria-hidden="true" data-mat-icon-type="font"> visibility_off </mat-icon><span _ngcontent-qte-c250="" fxshow="" fxhide.lt-sm="" translate="">dismiss</span></span><span matripple=""></span><span></span></button>

我什至如何去点击这个? 尝试做 this 链接所说的,但 Xpath 似乎不喜欢使用许多类

尝试了以下方法,似乎没有任何效果。

driver.find_element_by_xpath("//div[@aria-label=''Close Welcome Banner'']/div[@mat-focus-indicator close-dialog mat-raised-button mat-button-base mat-primary ng-start-inserted'']");
driver.find_element_by_xpath("//div[@aria-label=''Close Welcome Banner'']").click()
driver.findElement(By.xpath("//span[contains(@class,''middle'') and contains(text(),''Next'')]"))

有什么建议吗?

解决方法

看起来像 button 标签,而不是 div

试试这个:

driver.find_element_by_xpath("//button[@aria-label=''Close Welcome Banner'']").click()
,

您可以通过文本找到按钮标签的 xpath。

driver.find_element_by_xpath("//button[contains(text(),''Dismiss'')]")

今天的关于python selenium单击按钮python selenium 点击按钮的分享已经结束,谢谢您的关注,如果想了解更多关于Allegro Selenium 查找并单击按钮、ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、Python Selenium - 按钮未点击、Python Selenium 单击按钮,无名称、无 ID、无文本、许多类和 aria 标签的相关知识,请在本站进行查询。

本文标签: