Is it possible to srite something in fields with scrapy ?
For example I want to write my username and password in these fields.
I suggest you use the scrapy.http.FormRequest class.
Here's the documentation for it: http://doc.scrapy.org/en/latest/topics/request-response.html#formrequest-objects
Related
I am trying to display some data in my Django admin ReadOnlyFields, but I am unable to display, I want to display emailid and verify_emailid in the same line in the fields, I can do it using fieldset but it's displaying lable also, please let me know how I can display data without a table.
Here is my admin.py file...
class MyModelAdmin(ReadOnlyAdminMixin, CustomModelAdmin):
fields = (('emailid','verify_emailid),'mobilenumber','type')
here emailid and verify_emailid display in the same line but the issue is the verify_emailid display label, and I don't want the label of verify_emailid, please let me know how I can remove the verify_emailid label...
If you want complete control of the text for a particular line, you could add a method to control that line, and then add that method as a readonly field:
Solution
from django.utils.html import format_html
class MyModelAdmin(ReadOnlyAdminMixin, CustomModelAdmin):
fields = ('my_custom_line','mobilenumber','type')
def my_custom_line(self, instance):
return format_html('<p>{} {}</p>', instance.emailid, instance.verify_emailid)
How does the solution work
Any method on ModelAdmin class that returns some text, can be used as a readonly field. You can read more about this in the docs.
format_html is just a safe way of inserting html, to avoid various attacks. The values listed after the html are inserted into the html as would normally happen. You can edit this line anyway you like to get the desired affect. You can read about that here.
trying to do this on Instagram. there is code before it like importing the libraries and chrome driver path and logging in..so below is the loop that is failing...what's your suggestion? it works when I put in a list of exact URLs..so there is definitely something wrong in the loop
users=['instagramuser1','instagramuser2','instagramuser3']
user=-1
for user in users:
user+=1
webdriver.get('https://www.instagram.com/'+str(users)+'/')
sleep(5)
webdriver.find_element_by_css_selector('the_amazing_css_path').click()
I think what you want is:
users=['instagramuser1','instagramuser2','instagramuser3']
for user in users:
webdriver.get('https://www.instagram.com/' + user + '/')
sleep(5)
webdriver.find_element_by_css_selector('the_amazing_css_path').click()
Note user instead of users in the URL string.
I assume you use exact code posted in the question.
so you did not follow python indent rule in this particular code
it sould be fix like this:
users=['instagramuser1','instagramuser2','instagramuser3']
for user in users:
webdriver.get('https://www.instagram.com/'+user+'/')
sleep(5)
webdriver.find_element_by_css_selector('the_amazing_css_path').click()
I want to create function that will allow to fill registration, authorization and other text forms. Something like:
def fill_form(my_list):
forms = driver.find_elements_by_xpath(common_xpath)
for element in forms:
element.send_keys(my_list[forms.index(element)])
It should get as arguments a list of text values to send into <input type="text">, <input type="password"> and <textarea> elements.
So far I have:
common_xpath ='//input[#type="text" or #type="password"]'
but I can't understand how to add textarea element to this XPath to match it also
A simpler and more future-proof strategy would be separate the XPath expression into 2/3 distinct expressions that search for required WebElements, but if you really need to use a single XPath expression I imagine you could use the | operator to devise something along the lines of:
//input[#type="text" or #type="password"] | //textarea
Not sure if this is going to do what you look for, but, from what I can see this Xpath could get the job done:
common_xpath = "//*[self::input[#type='text'] or self::input[#type='password'] or self::textarea]"
I don't program in Python, but tried that one in Chrome's console (using $x("...")), and it seems to do what you want. You should consider calling that XPath inside the form (path/to/your/form//*...), to make it more specific.
TIL that you could select different tags in Xpath :)
Check this related answer for more info.
Finally, as a personal note, I'm not that experience with Selenium, but I would suggest you to consider using the PageObject model design pattern, to make the tests easier to maintain. Hope it works for you.
I am not aware of Python, but this is what I would do in a JAVa
String [] commanXpath= {"text", "password"};
String xpathVar= "//input[#type='"+commanXpath[index]+"']";
System.out.println(xpathVar);
By common_xpath= By.xpath(xpathVar);
See if you can implement similar logic in Python. Can you also update original post with exact html tags.
I am trying to pass email as parameter in django URL. I want to pass email as well as normal string and number also in URL as arguments.
url(r"search_connections/(?P<data>[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$", "search_connections", name="search_connections"),
It's working properly for email as a parameter. But for normal string like "abc" it's not working:
working for "/search_connections/abc#test.com/"
not working for "/search_connections/abc/"
I want this URL to work for both.
You may try simply use | (or) with \w+:
r'search_connections/(?P<data>\w+|[\w.%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$'
However I think regex for email just isn't a robust solution to match all valid emails.
How can I have URLs like example.com/category/catename-operation/ in Django?
Also in some cases the user enters a space separated category, how can I handle that?
E.g if the user enters the category as "my home", then the URL for this category will become
example.com/my home/ which is not a valid URL.
How can I handle these things?
If you want to keep your URLs pretty, for example when a user enters "my category" you could have "my-category" instead of "my%20category" in the URL. I suggest you look into SlugField (http://docs.djangoproject.com/en/dev/ref/models/fields/#slugfield) and prepopulating that slugfield using ModelAdmin's prepopulated_fields attribute.
http://example.com/my%20home/ is a valid URL where space character is escaped and Django will do all escaping/unescaping for you.
You can use the slugify template tag within your views to deal with spaces and such like so:
from django.template.defaultfilters import slugify
slugify("This is a slug!") # Will return u'this-is-a-slug'
You can try an improved version of SlugField called AutoSlugField which is part of Django Custom Management Command Extensions.
You could consider adding a URL-friendly name to your category and using that in the URL instead.
As another example you could have example.com/tv/ and have the category called "Televisions."
How can I handle these things?
If you want to handle this thing, to obtain my-url, then use the form field clean method to return the valid url. Thats what it is meant for.