硒我无法点击按钮python

时间:2021-04-16 10:10:19

标签: python selenium selenium-webdriver css-selectors

我尝试了很多方法都行不通你有什么解决方案吗?

<button type="button" class="btn btn-solid-primary btn--l _3Kiuzg" aria-disabled="false">buy</button>

我的代码和错误

browser.find_elements_by_css_selector(".btn-solid-primary").click()

AttributeError: 'list' object has no attribute 'click'

我可以试试

  • find_elements_by_class_name

不工作

browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")

错误

Traceback (most recent call last):
  File "D:\Project-Program\selenium\test3.py", line 11, in <module>
    browser.find_element_by_class_name("btn btn-solid-primary btn--l _3Kiuzg")
  File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "D:\Project-Program\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-solid-primary btn--l _3Kiuzg"}

2 个答案:

答案 0 :(得分:0)

这是答案 - 在下面的代码中将元素更改为元素。

 public void BindAllCartnulluser()
        {
            string GuestID = Request.QueryString["guestid"];
            try
            {
                SqlConnection con = new SqlConnection(CS);
                SqlCommand cmd = new SqlCommand("ProcDataBind_SelectCartGuest", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                cmd.Parameters.AddWithValue("@guestid", GuestID);

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                dtlistgrid.DataSource = ds;
                dtlistgrid.DataBind();

                decimal sum = 0; decimal qty = 0;
                for (int i = 0; i < dtlistgrid.Items.Count; i++)
                {
                    sum += Convert.ToDecimal(((Label)dtlistgrid.Items[i].FindControl("totalpricelbl")).Text);
                    qty += Convert.ToDecimal(((Label)dtlistgrid.Items[i].FindControl("qtylbl")).Text);
                }

                lblsubTotal.Text = sum.ToString();
                lbltotal.Text = sum.ToString();
                lblTotalQty.Text = qty.ToString();

                con.Close();
            }
            catch
            {

            }
        }

答案 1 :(得分:0)

此外,我假设您正在解析 Instagram。它使用动态类(在您的情况下,_3Kiuzg),在页面重新加载后会发生变化。所以请确保,您不是在寻找元素。函数 find_element_by_class_name 没有带很多类,所以,如果你想要这样,你应该使用 find_element_by_css_selector 和选择器 .class_1.class_2.etc