How to get the content of the class attribut with Python/XPATH? - python-3.x

Is it possible to get the content of the class attribut and how to implement this?

The class name it-self.
tform = driver.find_elements_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a[#class]")
Print(tform.text) returns the correct number of hits, but strings are empty

linkClass = driver.find_elements_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a[#href]").__getattribute__('class')
AttributeError: 'list' object has no attribute 'class'

Try this.
linkClass = driver.find_element_by_xpath("//table[#id = 'table-type-1']/tbody/tr/td[#class ='form col_form']/div/a").get_attribute('class')
print(linkClass)

Related

python - instantiate classes by looping through a dictionary

I'm quite new to Python, but come from a Lua background. I know exactly how I would achieve this in lua. I'm sure the answer already exists but I don't know how to ask the question - what terminology to search for? Things like 'dynamically define variables' return a lot of arguments, along with advice to use dictionaries as that is what they are for. But in my case dictionaries don't seem to work.
A condensed example, where Button(ID) is creating an instance of a button class:
Button1 = Button(8)
Button2 = Button(3)
Button3 = Button(432)
ButtonClose = Button(5004)
As there are more than 4 buttons in my actual UI, and I do more with them than just instantiate the class objects, I wish to use a loop structure of some sort to condense the code.
BtnList = {'Button1' : 8, 'Button2' : 3, 'Button3' : 432, 'ButtonClose' : 5004,}
for btn,ID in BtnList:
# some code here
I've tried using the following outside of any loops/functions, to avoid scoping issues while testing:
btns = {}
btns.Button1 = Button(8)
But this doesn't seem to be possible as I get an error:
AttributeError: 'dict' object has no attribute 'Button1'
Help please?
You have a problem with dict mapping. It should be changed to
btns = {}
btns["Button1"] = Button(8)
Then the string "Button1" inside the btns dictionary will contain the Button Object.

SimpleAttribute instead of Attribute in Rapidminer script?

I am trying to extract an Attribute from an ExampleSet in a RapidMiner 'Execute script' like this:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.getAttribute("h_area");
but then I get an error and it says that attrs is not a Attributes but a SimpleAttributes object.
This works:
Attribute[] attrs2 = exSet.createRegularAttributeArray();
Attribute attr2 = attrs2.getAt(1);
What is the correct way to get an Attribute from an ExampleSet?
From these docs, it looks like the getAttributes() call will return an object implementing the Attributes abstract class, which SimpleAttributes is, so it looks pretty fair at this stage. However, the getAttribute() method doesn't look like it's defined in either object. I can't test this here and now, but have you tried the following:
ExampleSet exSet = input[0];
Attributes attrs = exSet.getAttributes();
Attribute attr = attrs.get("h_area");

How to create a new object using a class name in Java ME?

So far I couldn't find a method which doesn't require the Constructor class.
This class is not available in Java ME.
Is there any other way?
Note that the class's constructor takes parameters.
Is this what you are looking for?
String className = "java.lang.String";
Class theClass = Class.forName(className);
// this line will call the empty constructor
String s = (String) theClass.newInstance();
http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/java/lang/Class.html#newInstance%28%29

How can I create Dictionary Type of Object from JSON type of string

I am looking to create a dictionary type of object from below string without using any extension class. I would prefer to write a .net class which will do serialize and deserialize it.
string userDetails = "{"FullName":"Manoj Singh","username":"Manoj","Miles":2220,"TierStatus":"Gold","TierMiles":23230,"MilesExpiry":12223,"ExpiryDate":"31 January 2011","PersonID":232323,"AccessToken":"sfs23232s232","ActiveCardNo":"232323223"}";
I have got above string in my results, now I want to convert it into dictionary type of Object using .NET 2.0.
Thanks.
I was waiting since many days to have any response on it, unfortunately there were no response, well guys I resolved my problem with the help of this question Can you Instantiate an Object Instance from JSON in .NET?
Thanks to all you who have seen this question!
try this!!
Class deserialize<Class>(string jsonStr, Class obj) { /* ... */}
var jstring = "{fname='Test', lname='test, oth='test'}";
var p = deserialize(jstring, new {fname="",lname=""});
var x = person.fname; //strongly-typed

Magento: getting attributes from an attribute set without a product

I have set an attribute set in my Magento shop which has several binary attributes.
For a pulldown I need a list of ALL the attributes inside this one attribute set, including their internal name and their label. Since this pulldown should appear in places that not necessarily have a product selected I can't go the usual route of "getting the attributes of a product".
How do I go about of getting a list of all the attributes inside my set?
OK, I realised that I missed that you want the whole set of attributes, not just an individual one. Try this:
$productEntityType = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY);
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection');
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($productEntityType->getId()) //4 = product entities
->addSetInfo()
->getData();
You'll then need to iterate through the array that is returned with something like:
foreach($attributesInfo as $attribute):
$attribute = Mage::getModel('eav/entity_attribute')->load($attribute['attribute_id']);
echo 'label = '.$attribute->getFrontendLabel().'<br/>';
echo 'code = '.$attribute->getAttributeCode().'<br/><br/>';
endforeach;
Sorry for missing the original point, hope this helps!
Cheers,
JD
In order to get all the attributes in an attribute set, you can use as:
$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Default'; //Edit with your required Attribute Set Name
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
foreach($attributes as $_attribute){
    print_r($_attribute);
}
Cheers!!
try this snippet, it should give you want you need, except for multi-select attributes.
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product','attribute_name');
foreach($attribute->getSource()->getAllOptions(true,true) as $option){
$attributeArray[$option['value']] = $option['label'];
}
return $attributeArray;
Hope this helps,
JD

Resources