Entering an input, clicking it and retrieving particular information with Selenium [on hold]



  • I\'m new to web scraping with Python. My intent is to retrieve the verb for a word of interest. For e.g. dictionary.com has definitions for different parts of speech for word, I would like to enter a word of interest and then hit the search icon, in the resulting page I would like to extract the information under the header \'verb\'.



  • To extract the information under verb header Induce WebDriverWait and presence_of_all_elements_located()

    Here is the code.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver=webdriver.Chrome()
    driver.get("https://www.dictionary.com/")
    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Accept Cookies')]"))).click()
    elementsearch=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[title='Search']")))
    elementsearch.send_keys("interest")
    elementsearch.submit()
    results=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='luna-pos'][contains(.,'verb')]/following::div[1]//div[@class='default-content']//div")))
    for item in results:
        print(item.text)
    
    results1=WebDriverWait(driver,20).until(EC.presence_of_all_elements_located((By.XPATH,"//span[@class='luna-pos'][contains(.,'verb')]/following::div[1]//div[@class='expandable-content']//div")))
    for item in results1:
        print(item.get_attribute("textContent"))
    
    

    Output on console:

    to engage or excite the attention or curiosity of:
    Mystery stories interested him greatly.
    to concern (a person, nation, etc.) in something; involve:
    The fight for peace interests all nations.
    to cause to take a personal concern or share; induce to participate: to interest a person in an enterprise.
    to cause to be concerned; affect.
    
    


最新内容

  • 问题

    I have a Provider which has a method which takes data from Firebase as a stream, transforms it to a list and returns a Stream<List<Model>> . I\'m trying to write a test where I want to check if the items in the List are the same as I expect them to be. How can I do that?

    My Current Code:

    test(\'getContacts returns a empty list when there is no contact\',() async{ when(sharedPreferencesMock.get(any)).thenReturn(\'uid\'); //mock the sharedprefs documentSnapshot = DocumentSnapshotMock(); //mock documentsnapshot when(documentSnapshot.exists).thenReturn(true); // this is done to pass the getUidByUsername method documentReference = DocumentReferenceMock(documentSnapshotMock: documentSnapshot); documentReference.setData({ \'uid\':\'uid\', \'contacts\':[] // setting the usename in the data already so that duplicate contact exception is thrown }); userDataProvider.getContacts().asBroadcastStream().listen((data){ expect(data.length,0); }); });

    And the provider method

    @override Stream<List<Contact>> getContacts() { CollectionReference userRef = fireStoreDb.collection(Paths.usersPath); DocumentReference ref = userRef.document(SharedObjects.prefs.get(Constants.sessionUid)); return ref.snapshots().transform(StreamTransformer<DocumentSnapshot, List<Contact>>.fromHandlers(handleData: (documentSnapshot, sink) async{ List<String> contacts; if (documentSnapshot.data[\'contacts\'] == null) { ref.updateData({\'contacts\': []}); contacts = List(); } else { contacts = List.from(documentSnapshot.data[\'contacts\']); } List<Contact> contactList = List(); for (String username in contacts) { print(username); String uid = await getUidByUsername(username); DocumentSnapshot contactSnapshot = await userRef.document(uid).get(); contactList.add(Contact.fromFirestore(contactSnapshot)); } sink.add(contactList); })); }

    Update:

    StreamController streamController = StreamController<List<Contact>>(); StreamSink<List<Contact>> sink = streamController.sink; Stream<List<Contact>> stream = streamController.stream; stream.listen((List<Contact> list){ expect(list.length,1); }); userDataProvider.mapDocumentToContact(userCollection, userRef, documentSnapshot, sink); streamController.close(); 回答1:

    1

    Make the lambda function that you currently pass to the StreamTansformer a separate function and test that. If you want to test the full function there is a Firebase mock package on pub.

    share|improve this answer

    answered Sep 13 at 7:00

    ThomasThomas 3,08033 gold badges1717 silver badges3131 bronze badges

    Thanks a lot! I extracted the lambda function out and wrote the test for it. It's passing now. I have updated the question with the code snippet. Can you check if it's the right way of doing it? – Aditya Gurjar Sep 13 at 10:08 Please mark ma post as answer then. – Thomas Sep 13 at 11:38 Done. Thanks @Thomas – Aditya Gurjar Sep 13 at 19:31

    add a comment |

    read more
  • Make the lambda function that you currently pass to the StreamTansformer a separate function and test that. If you want to test the full function there is a Firebase mock package on pub.

    read more
  • I have a Provider which has a method which takes data from Firebase as a stream, transforms it to a list and returns a Stream<List<Model>> . I\'m trying to write a test where I want to check if the items in the List are the same as I expect them to be. How can I do that?

    My Current Code:

    test(\'getContacts returns a empty list when there is no contact\',() async{ when(sharedPreferencesMock.get(any)).thenReturn(\'uid\'); //mock the sharedprefs documentSnapshot = DocumentSnapshotMock(); //mock documentsnapshot when(documentSnapshot.exists).thenReturn(true); // this is done to pass the getUidByUsername method documentReference = DocumentReferenceMock(documentSnapshotMock: documentSnapshot); documentReference.setData({ \'uid\':\'uid\', \'contacts\':[] // setting the usename in the data already so that duplicate contact exception is thrown }); userDataProvider.getContacts().asBroadcastStream().listen((data){ expect(data.length,0); }); });

    And the provider method

    @override Stream<List<Contact>> getContacts() { CollectionReference userRef = fireStoreDb.collection(Paths.usersPath); DocumentReference ref = userRef.document(SharedObjects.prefs.get(Constants.sessionUid)); return ref.snapshots().transform(StreamTransformer<DocumentSnapshot, List<Contact>>.fromHandlers(handleData: (documentSnapshot, sink) async{ List<String> contacts; if (documentSnapshot.data[\'contacts\'] == null) { ref.updateData({\'contacts\': []}); contacts = List(); } else { contacts = List.from(documentSnapshot.data[\'contacts\']); } List<Contact> contactList = List(); for (String username in contacts) { print(username); String uid = await getUidByUsername(username); DocumentSnapshot contactSnapshot = await userRef.document(uid).get(); contactList.add(Contact.fromFirestore(contactSnapshot)); } sink.add(contactList); })); }

    Update:

    StreamController streamController = StreamController<List<Contact>>(); StreamSink<List<Contact>> sink = streamController.sink; Stream<List<Contact>> stream = streamController.stream; stream.listen((List<Contact> list){ expect(list.length,1); }); userDataProvider.mapDocumentToContact(userCollection, userRef, documentSnapshot, sink); streamController.close();

    read more

最新主题

推荐阅读

本站部分内容来自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请联系我们,我们将及时予以处理。
Powered by NodeBB | 备案号:宁ICP备15000671号