如何使用Selenium和Python按HTML单击网页上的元素

时间:2018-08-03 06:39:15

标签: python selenium selenium-webdriver xpath css-selectors

我已经尝试了一段时间,但似乎无法使用python硒找到该元素。我使用过的所有方法(无论是通过xpath,css选择器还是通过类找到)都返回否定的,所以有人知道如何查找并单击中间值为“ LOG IN”和类型为“ button”的元素吗? / p>

firefox_options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(firefox_options=firefox_options)
driver.get('https://www.nike.com/launch/?cp=usns_aff_nike&s=upcoming')
time.sleep(.2)
logIn = driver.find_element_by_css_selector('#root > div > div > div.main-       layout > div > header > div.d-sm-h.d-lg-b > section > ul > li.member-nav-item.d-sm-ib.va-sm-m > button')
logIn.click()
time.sleep(.2)
email = driver.find_element_by_name('emailAddress')
email.send_keys('email')
passWord = driver.find_element_by_name('password')
passWord.send_keys('password')

# Find and click element from above

我似乎找不到它,因此,感谢您的帮助!

public class GoogleLoginRendere : PageRenderer, 
GoogleApiClient.IOnConnectionFailedListener, Android.Views.View.IOnClickListener
{
    GoogleApiClient mGoogleApiClient;
    MainActivity activity;
    SignInButton signInButton;
    Android.Widget.Button btn;
    public GoogleLoginRendere()
    {

    }
    public void OnConnectionFailed(ConnectionResult result)
    {
        throw new NotImplementedException();
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
    base.OnElementChanged(e);
    activity = Context as MainActivity;
    if (e.OldElement != null)
    {
        activity.ActivityResult -= HandleActivityResult;
    }
    if (e.NewElement != null)
    {
        activity.ActivityResult += HandleActivityResult;
    }
    btn = new Android.Widget.Button(activity);
    btn.Text = "Google SignIn";
    signInButton = new SignInButton(activity);
    signInButton.SetSize(SignInButton.SizeStandard);
    signInButton.Visibility = ViewStates.Visible;

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
     .RequestEmail()
     .Build();

    mGoogleApiClient = new GoogleApiClient.Builder(activity)
     .EnableAutoManage(activity /* FragmentActivity */, this /* OnConnectionFailedListener */)
     .AddApi(Auth.GOOGLE_SIGN_IN_API, gso)
     .Build();

    FrameLayout.LayoutParams linLayoutParam = new FrameLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
    linLayoutParam.Gravity = GravityFlags.Center;

    activity.AddContentView(btn, linLayoutParam);
    btn.SetOnClickListener(this);

    /* signInButton.Click += (sender, ex) =>
     {
      Console.WriteLine(string.Format("Click called"));
         Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
         activity.StartActivityForResult(signInIntent, 0);
     };*/

    MessagingCenter.Subscribe<object, string>(this, "CallLogout", (sender, msg) =>
    {
        Auth.GoogleSignInApi.SignOut(mGoogleApiClient);
        signInButton.Visibility = ViewStates.Visible;
    });
    }

    private void HandleActivityResult(object sender, ActivityResultEventArgs e)
    {
        Console.WriteLine(string.Format("renderer called"));
        if (e.RequestCode == 0)
        {
        GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(e.Data);
        if (result.IsSuccess)
        {
            signInButton.Visibility = ViewStates.Invisible;
            User aUser = new User();
            aUser.Email = result.SignInAccount.Email;
            App.appUser = aUser;
            MessagingCenter.Send<object, string>(this, "callLoginWebservice", "callLoginWebservice");
        }
        else
        {
            Toast.MakeText(activity, "email not found", ToastLength.Long).Show();
            Auth.GoogleSignInApi.SignOut(mGoogleApiClient);
            signInButton.Visibility = ViewStates.Visible;
        }
        }
    }
    public void OnClick(Android.Views.View v)
    {
        var signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
        activity.StartActivityForResult(signInIntent, 0);
        //Intent signInIntent = Auth.GoogleSignInApi.GetSignInIntent(mGoogleApiClient);
        //((MainActivity)Forms.Context).StartActivityForResult(signInIntent, 1);
        //mGoogleApiClient.Connect();
    }
}

在选择登录按钮时,其他所有功能都可以正常工作。这是代码,以防万一他们可能有问题或有人想检查一下,谢谢

2 个答案:

答案 0 :(得分:1)

要在文本为登录的元素上调用click(),您需要诱使 WebDriverWait 使元素可点击您可以使用以下任一解决方案:

  • css_selector

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.nike-unite-submit-button.loginSubmit.nike-unite-component.blurred > input[value='LOG IN']"))).click()
    
  • xpath

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='nike-unite-submit-button loginSubmit nike-unite-component blurred']/input[@value='LOG IN']"))).click()
    

答案 1 :(得分:0)

您可以使用按钮的ID找到该按钮,然后单击它:

button = driver.find_element_by_id('019a8673-60aa-4b9f-825a-00b01ad36507')
button.click()

在这里,驱动程序是您的浏览器网络驱动程序。