PDA

View Full Version : سوال: کلیک کردن یک دکمه در یک صفحه وب



Hamishebahar
چهارشنبه 31 شهریور 1389, 10:32 صبح
سلام دوستان خسته نباشید.
من با WebRequest یک صفحه رو میگیرم حالا میخوام مثلاً روی یک دکمه کلیک کنم و به صفحه بعد برم.
مثلاً سایت رپیدشر وی دکمه Free کلیک کنم و بعد وارد صفحه دانلود بشم.
یا مثلاً نام کاربر و پسوردمو توی برنامه نویس بنویسم و روی دکمه ورود کلیک کنم و به صفحه بعد برم.
کسی میدونه چطوری اینکارو بکنم؟
ممنون

fjm11100
چهارشنبه 31 شهریور 1389, 13:17 عصر
ببین به این راحتی هم نیس. بستگی داره اون دکمه چجوری هندل میشه، با یک روال جاوا اسکریپت توی همون صفحه، با Get یا Post به یک صفحه دیگه

bade saba
چهارشنبه 31 شهریور 1389, 13:41 عصر
من کدی که تو لینک زیر گذاشتم رو برای کار مشابهی از سایت ماکروسافت پیدا کردم که تو این کد فیلد name و password وارد میشه و دکمه s1 کلیک میشه اما اون ایرادی که نوشتم رو میگیره اگه تونستی ایراد رو رفع کنی ممنون میشم روش رو بگی

http://www.barnamenevis.org/forum/showthread.php?t=248492

reza2012
چهارشنبه 31 شهریور 1389, 13:53 عصر
بحث جالبیه منتظر نتیجه هستم

bade saba
چهارشنبه 31 شهریور 1389, 14:18 عصر
کد زیر کاملا کار میکنه البته از اون کلاسی که شما استفاده کردی استفاده نشده اما پر کردن فیلد و کلیک کردن رو به راحتی انجام میده


if (webBrowser1.Document != null)
{
HtmlElementCollection elements = webBrowser1.Document.Forms["login"].All;
foreach (HtmlElement element in elements)
switch (element.GetAttribute("name"))
{
case "login":
element.SetAttribute("value", "loginName");
break;
case "pass":
element.SetAttribute("value", "123456");
break;
}
foreach (HtmlElement element in elements)
if(element.GetAttribute("name") == "submit")
element.InvokeMember("click");
}

فکر کنم نیاز به گفتن نیست که url صفحه ای که باید کلیک انجام بشه یا فیلد وارد بشه قبلا یاید ست بشه و صفحه آماده باشه

Hamishebahar
چهارشنبه 31 شهریور 1389, 16:43 عصر
ممنونم دوست من.
ولی اگه قرار بود از WebBrowser بگیرم که اصلاً سوالمو مطرح نمیکردم.
من از یکی از بچه ها سوال کردم چیز زیاد خاصی نداشت با همون WebRequest انجام میشد.ولی به دلیل مشغله کاری نتونست بهم بگه:ناراحت:

bade saba
چهارشنبه 31 شهریور 1389, 17:52 عصر
using System;
using System.Net;
using System.IO;
using System.Text;using System.Threading;

public class RequestState
{
// This class stores the request state of the request.
public WebRequest request;
public RequestState()
{
request = null;
}
}

class WebRequest_BeginGetRequeststream
{
public static ManualResetEvent allDone= new ManualResetEvent(false);
static void Main()
{
// Create a new request to the mentioned URL.
WebRequest myWebRequest= WebRequest.Create("http://www.contoso.com");

// Create an instance of the RequestState and assign
// 'myWebRequest' to it's request field.
RequestState myRequestState = new RequestState();
myRequestState.request = myWebRequest;
myWebRequest.ContentType="application/x-www-form-urlencoded";

// Set the 'Method' property to 'POST' to post data to a Uri.
myRequestState.request.Method="POST";
// Start the Asynchronous 'BeginGetRequestStream' method call.
IAsyncResult r=(IAsyncResult) myWebRequest.BeginGetRequestStream(
new AsyncCallback(ReadCallback),myRequestState);
// Pause the current thread until the async operation completes.
Console.WriteLine("main thread waiting...");
allDone.WaitOne();
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse = myWebRequest.GetResponse();
Console.WriteLine("The string has been posted.");
Console.WriteLine("Please wait for the response...");

Stream streamResponse = myWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe contents of the HTML page are ");

while (count > 0)
{
String outputData = new String(readBuff, 0, count);
Console.Write(outputData);
count = streamRead.Read(readBuff, 0, 256);
}

// Close the Stream Object.
streamResponse.Close();
streamRead.Close();


// Release the HttpWebResponse Resource.
myWebResponse.Close();
}
private static void ReadCallback(IAsyncResult asynchronousResult)
{
RequestState myRequestState =(RequestState) asynchronousResult.AsyncState;
WebRequest myWebRequest = myRequestState.request;

// End the Asynchronus request.
Stream streamResponse = myWebRequest.EndGetRequestStream(asynchronousResul t);

// Create a string that is to be posted to the uri.
Console.WriteLine("Please enter a string to be posted:");
string postData = Console.ReadLine();
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Write the data to the stream.
streamResponse.Write(byteArray,0,postData.Length);
streamResponse.Close();
allDone.Set();
}
}


این رو امتحان کن

Hamishebahar
چهارشنبه 31 شهریور 1389, 19:25 عصر
راستش من نمیدونم چطوری از کدهای بالا استفاده کنم.
دوستان من کد زیر رو نوشتم از همون تاپیکی که دوست عزیز معرفی کردن کپی گرفتم میخوام وارد سایت http://www2.khanwars.ir این یاست هم نام کاربری داره هم پسورد و هم یه کمبو باکس ممنون میشم کمکم کنید:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www2.khanwars.ir/");

webRequest.SendChunked = true;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

ASCIIEncoding encoding = new ASCIIEncoding();
// byte[] data = encoding.GetBytes("worldLinks1&user=myuser&pass=mypassword&s1=Button");
byte[] data = encoding.GetBytes("track=&world_select=1&user=user123&pass=password123");
webRequest.ContentLength = data.Length;


webRequest.CookieContainer = new CookieContainer();


Stream newStream = webRequest.GetRequestStream();//**********Error*****
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();



string html;
using (Stream strmresponse = webResponse.GetResponseStream())
{
using (StreamReader reader = new StreamReader(strmresponse, Encoding.UTF8))
{
html = reader.ReadToEnd();
}
}